Build sources to containers

Cloud Run supportsdirectly deploying source code,however, you can also build your source code into a container image and thendeploy this container image to Cloud Run. You can use container imagesfor any Cloud Run resource.

Common use cases for decoupling build and deploy operations:

  • Continuous Integration and Delivery: Developers author and push codeto a source repository, a CI/CD system automatically builds this source code intoa container, runs tests, and automatically deploys it to a staging environment.
  • Infrastructure as Code: Cloud Run resources that are managed usingYAML or Terraform reference a container image URL. The source code writtenby developers need to be built into a container image.

You can use any system you want to build a container. This page describesthe following ways to use Cloud Build to build container images:

Requirements for Cloud Run services

For Cloud Run services, you can use container images built with any tool capable ofbuilding container images, as long as they respect thecontainer contract.In particular, your code must listen for HTTP requests on the port defined bythePORT environment variable. ThisPORT environment variable isautomatically injected by Cloud Run into your container.

Before you begin

Build using a Dockerfile

Caution: Don't include the file~/.config/gcloud/gce in your image. This file isgenerated if you run Google Cloud CLI commands as part of your image build. It cancause authentication issues when the image runs in Cloud Run.

Before building your sources into a container image ("containerizing") locallyusing Docker or using Cloud Build, you need aDockerfile to be presentalong with your sources.TheHello World samples contain sample applications and Dockerfiles in many popular languages.

If you use Dockerfiles, you can use either of the following methods to build:

  • Build using Cloud Build
  • Build locally using Docker

Build using Cloud Build

You can build your image on Google Cloud by usingCloud Build:

  1. Navigate to the folder containing your sources and Dockerfile.

  2. Run the command:

    gcloudbuildssubmit--tagIMAGE_URL

    ReplaceIMAGE_URL with a reference to the container image, forexample,us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,therepositoryREPO_NAME mustalready be created. The URL follows the format ofLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.

For tips on improving build performance, seeSpeeding up your builds

Build locally and push using Docker

If you have Dockerinstalled locally, youcan usedocker buildinstead of using Cloud Build or Google Cloud's buildpacks.

To build your container image using Docker:

  1. Navigate to the folder containing your sources andDockerfile.

  2. Run the command:

    dockerbuild.--tagIMAGE_URL

    ReplaceIMAGE_URL with a reference to the container image, forexample,us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,therepositoryREPO_NAME mustalready be created. The URL follows the format ofLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.

    Note that if you are using a Mac with Apple silicon, you must specify--platform linux/amd64in the command line.

  3. Push the container image to a supported container registry:

    dockerpushIMAGE_URL

    ReplaceIMAGE_URL with a reference to the container image, forexample,us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,therepositoryREPO_NAME mustalready be created. The URL follows the format ofLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.

To exclude local files from this process, follow the.dockerignore configuration file instructions.

Build using Google Cloud's buildpacks

Google Cloud's buildpacks is a set ofCNCF-compatible Buildpacks that build source code intocontainer images designed to run on Google Cloud container platforms,including Cloud Run.

For a list of supported languages, refer to theGoogle Cloud's buildpacks documentation

Build with Google Cloud's buildpacks using Cloud Build

To build with a Google Cloud's buildpacks:

  1. Navigate to the folder containing your sources.

  2. Run the command:

    gcloud builds submit --pack image=IMAGE_URL

    ReplaceIMAGE_URL with a reference to the container image, forexample,us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,therepositoryREPO_NAME mustalready be created. The URL follows the format ofLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.

  3. Wait for the build to complete.

Build with Google Cloud's buildpacks using thepack command line

To build using thepack command:

  1. If you haven't already done so,install Docker.

  2. If you haven't already done so,installpack.

  3. Navigate to the folder containing your sources.

  4. Run the following command to build and push to your supported container registry:

    pack build --publishIMAGE_URL

    ReplaceIMAGE_URL with a reference to the container image, forexample,us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,therepositoryREPO_NAME mustalready be created. The URL follows the format ofLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG.

  5. Wait forpack to finish.

For more information, read the instructions underBuilding an Application.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.