[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],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)."]]