Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Cloud Run akzeptiert Container-Images, die mit einem beliebigen Tool erstellt wurden, das Container-Images erstellen kann, solange diese den Containervertrag berücksichtigen.
Insbesondere muss Ihr Code den mit der Umgebungsvariable PORT definierten Port auf HTTP-Anfragen überwachen. Diese Umgebungsvariable PORT wird von Cloud Run automatisch in Ihren Container eingefügt.
Auf dieser Seite werden mehrere Möglichkeiten zum Erstellen von Container-Images beschrieben:
Dockerfile verwenden
Buildpacks von Google Cloud verwenden
Hinweise
Sie benötigen die Google Cloud CLI, um einige der Befehle auf dieser Seite auszuführen.
Bevor Sie Ihre Quellen mit Docker oder mit Cloud Build in ein Container-Image verpacken („containerisieren“), benötigen Sie zusätzlich ein Dockerfile.
Die Hello World-Beispiele enthalten Beispielanwendungen und Dockerfiles in vielen gängigen Sprachen.
Wenn Sie Dockerfiles verwenden, können Sie eine der folgenden Methoden zum Erstellen nutzen:
Mit Cloud Build erstellen
Lokal mit Docker erstellen
Mit Cloud Build erstellen
Sie können das Image mithilfe von Cloud Build in Google Cloud erstellen:
Öffnen Sie den Ordner, der die Quellen und das Dockerfile enthält.
Führen Sie diesen Befehl aus:
gcloudbuildssubmit--tagIMAGE_URL
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. us-docker.pkg.dev/cloudrun/container/hello:latest. Wenn Sie Artifact Registry verwenden, muss das RepositoryREPO_NAME bereits erstellt sein. Die URL hat die Form LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.
Lokal erstellen und mit Docker per Push übertragen
Wenn Sie Docker lokal installiert haben, können Sie docker build anstelle von Cloud Build oder den Buildpacks von Google Cloud verwenden.
So erstellen Sie Container-Image mit Docker:
Öffnen Sie den Ordner, der die Quellen und das Dockerfile enthält.
Führen Sie diesen Befehl aus:
dockerbuild.--tagIMAGE_URL
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. us-docker.pkg.dev/cloudrun/container/hello:latest. Wenn Sie Artifact Registry verwenden, muss das RepositoryREPO_NAME bereits erstellt sein. Die URL hat die Form LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.
Wenn Sie einen Mac mit Apple-Prozessor verwenden, müssen Sie --platform linux/amd64 in der Befehlszeile angeben.
Übertragen Sie das Container-Image per Push an eine unterstützte Container-Registry:
dockerpushIMAGE_URL
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. us-docker.pkg.dev/cloudrun/container/hello:latest. Wenn Sie Artifact Registry verwenden, muss das RepositoryREPO_NAME bereits erstellt sein. Die URL hat die Form LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.
Google Cloud Buildpacks sind eine Reihe von CNCF-kompatiblen Buildpacks, die Quellcode in Container-Images erzeugen, die für die Ausführung auf Google Cloud-Containerplattformen, einschließlich Cloud Run, entwickelt wurden.
Mit Buildpacks von Google Cloud mithilfe von Cloud Build erstellen
So erstellen Sie mit Buildpacks von Google Cloud:
Öffnen Sie den Ordner, der die Quellen enthält.
Führen Sie diesen Befehl aus:
gcloud builds submit --pack image=IMAGE_URL
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. us-docker.pkg.dev/cloudrun/container/hello:latest. Wenn Sie Artifact Registry verwenden, muss das RepositoryREPO_NAME bereits erstellt sein. Die URL hat die Form LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.
Warten Sie, bis der Build abgeschlossen ist.
Mit Buildpacks von Google Cloud über die pack-Befehlszeile erstellen
Führen Sie den folgenden Befehl aus, um einen Build zu erstellen und per Push in die unterstützte Container-Registry zu übertragen:
pack build --publish IMAGE_URL
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. us-docker.pkg.dev/cloudrun/container/hello:latest. Wenn Sie Artifact Registry verwenden, muss das RepositoryREPO_NAME bereits erstellt sein. Die URL hat die Form LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.
Warten Sie, bis pack abgeschlossen ist.
Weitere Informationen finden Sie in der Anleitung unter Anwendung erstellen.
Nächste Schritte
Nachdem der Container erstellt wurde, können Sie ihn lokal testen, bevor Sie ihn in Cloud Run bereitstellen. Weitere Informationen finden Sie unter Cloud Run-Dienst lokal testen.
Im Containervertrag können Sie nachlesen, welche Bedingungen Container für eine Bereitstellung in Cloud Run einhalten müssen.
Unter Dienste bereitstellen wird erklärt, wie Sie Container in Cloud Run bereitstellen.
Durch die Einrichtung einer kontinuierlichen Bereitstellung und die Verwendung von Cloud-Build-Triggern können Sie die Build-Erstellung und die Bereitstellung von Cloud Run-Diensten automatisieren.
Informationen zum Durchführen optimaler Container-Builds für Java-Anwendungen finden Sie unter Java-Container mit Jib erstellen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2024-12-05 (UTC)."],[],[],null,["# Build sources to containers\n\nCloud Run supports [directly deploying source code](/run/docs/deploying-source-code),\nhowever, you can also build your source code into a container image and then\ndeploy this container image to Cloud Run. You can use container images\nfor any Cloud Run resource.\n\nCommon use cases for decoupling build and deploy operations:\n\n- **Continuous Integration and Delivery**: Developers author and push code to a source repository, a CI/CD system automatically builds this source code into a container, runs tests, and automatically deploys it to a staging environment.\n- **Infrastructure as Code**: Cloud Run resources that are managed using YAML or Terraform reference a container image URL. The source code written by developers need to be built into a container image.\n\nYou can use any system you want to build a container. This page describes\nthe following ways to use Cloud Build to build container images:\n\n- [Using a Dockerfile](#use-dockerfile)\n- [Using Google Cloud's buildpacks](#buildpacks)\n\nRequirements for Cloud Run services\n-----------------------------------\n\nFor Cloud Run services, you can use container images built with any tool capable of\nbuilding container images, as long as they respect the [container contract](/run/docs/reference/container-contract).\nIn particular, your code must listen for HTTP requests on the port defined by\nthe `PORT` environment variable. This `PORT` environment variable is\nautomatically injected by Cloud Run into your container.\n\nBefore you begin\n----------------\n\n- You need the [Google Cloud CLI](/run/docs/setup) to run some of the commands in this page.\n\n- Create a repository at a [supported container registry](/run/docs/deploying#images). To\n [create an Artifact Registry repository](/artifact-registry/docs/repositories/create-repos#create),\n run:\n\n gcloud artifacts repositories create \u003cvar translate=\"no\"\u003eREPOSITORY\u003c/var\u003e \\\n --repository-format=docker \\\n --location=\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e \\\n --description=\"\u003cvar translate=\"no\"\u003eDESCRIPTION\u003c/var\u003e\" \\\n --immutable-tags \\\n --async\n\n- You can configure Docker to get access to Artifact Registry using the\n [gcloud CLI credential helper](/artifact-registry/docs/docker/authentication#gcloud-helper):\n\n ```bash\n gcloud auth configure-docker LOCATION-docker.pkg.dev\n ```\n Replace \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e with the region name of your container repository, for example, `us-west2`.\n\n \u003cbr /\u003e\n\nBuild using a Dockerfile\n------------------------\n\n| **Caution:** Don't include the file `~/.config/gcloud/gce` in your image. This file is generated if you run Google Cloud CLI commands as part of your image build. It can cause authentication issues when the image runs in Cloud Run.\n\nBefore building your sources into a container image (\"containerizing\") locally\nusing Docker or using Cloud Build, you need a\n[Dockerfile](https://docs.docker.com/engine/reference/builder/) to be present\nalong with your sources.\nThe [Hello World samples](https://github.com/GoogleCloudPlatform/cloud-run-samples/blob/main/README.md)\ncontain sample applications and Dockerfiles in many popular languages.\n\nIf you use Dockerfiles, you can use either of the following methods to build:\n\n- Build using Cloud Build\n- Build locally using Docker\n\n### Build using Cloud Build\n\nYou can build your image on Google Cloud by using [Cloud Build](/build/docs):\n\n1. Navigate to the folder containing your sources and Dockerfile.\n\n2. Run the command:\n\n ```bash\n gcloud builds submit --tag IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n\nFor tips on improving build performance, see\n[Speeding up your builds](/build/docs/speeding-up-builds)\n\n### Build locally and push using Docker\n\nIf you have Docker [installed locally](https://docs.docker.com/install/), you\ncan use [`docker build`](https://docs.docker.com/engine/reference/commandline/build/)\ninstead of using Cloud Build or Google Cloud's buildpacks.\n\nTo build your container image using Docker:\n\n1. Navigate to the folder containing your sources and `Dockerfile`.\n\n2. Run the command:\n\n ```bash\n docker build . --tag IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n\n Note that if you are using a Mac with Apple silicon, you must specify `--platform linux/amd64`\n in the command line.\n3. Push the container image to a supported container registry:\n\n ```bash\n docker push IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n\nTo exclude local files from this process, follow the\n[`.dockerignore` configuration file](https://docs.docker.com/engine/reference/builder/#dockerignore-file)\ninstructions.\n\nBuild using Google Cloud's buildpacks\n-------------------------------------\n\n[Google Cloud's buildpacks](/docs/buildpacks/builders) is a set of\n[CNCF-compatible Buildpacks](https://buildpacks.io) that build source code into\ncontainer images designed to run on Google Cloud container platforms,\nincluding Cloud Run.\n\nFor a list of supported languages, refer to the [Google Cloud's buildpacks documentation](/docs/buildpacks/builders)\n\n### Build with Google Cloud's buildpacks using Cloud Build\n\nTo build with a Google Cloud's buildpacks:\n\n1. Navigate to the folder containing your sources.\n\n2. Run the command:\n\n ```\n gcloud builds submit --pack image=IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n3. Wait for the build to complete.\n\n### Build with Google Cloud's buildpacks using the `pack` command line\n\nTo build using the [pack command](/docs/buildpacks/build-application):\n\n1. If you haven't already done so, [install Docker](https://docs.docker.com/install/).\n\n2. If you haven't already done so, [install `pack`](https://buildpacks.io/docs/tools/pack/cli/install/).\n\n3. Navigate to the folder containing your sources.\n\n4. Run the following command to build and push to your supported container registry:\n\n ```\n pack build --publish IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n5. Wait for `pack` to finish.\n\nFor more information, read the instructions under\n[Building an Application](/docs/buildpacks/build-application).\n\nWhat's next\n-----------\n\n- To deploy your built containers to Cloud Run, follow [Deploying services](/run/docs/deploying).\n\n- Learn how to [create and update Cloud Run jobs](/run/docs/create-jobs)\n from your built container image.\n\n- After your container has been built, you can test locally before deploying to\n Cloud Run; see [Testing a Cloud Run service locally](/run/docs/testing/local) to learn more.\n\n- After you create or update a job using the built container, see how to\n [execute the job](/run/docs/execute/jobs) as a one-off, on a schedule, or as\n part of a [workflow](/run/docs/triggering/invoke-jobs-with-workflows).\n\n- To automate the builds and deployments of your Cloud Run services\n using Cloud Build Triggers, [set up continuous deployment](/run/docs/continuous-deployment).\n\n- To perform optimal container builds for Java application, see\n [Building Java containers with Jib](/java/getting-started/jib)."]]