Building Containers

Cloud Run accepts container images built with any tool capable of building container images, as long as they respect the container contract. In particular, your code must listen for HTTP requests on the port defined by the PORT environment variable. This PORT environment variable is automatically injected by Cloud Run into your container.

This page describes several ways to build container images:

  • Using a Dockerfile
  • Using Buildpacks

Building using a Dockerfile

Before building your sources into a container image ("containerizing") locally using Docker or using Cloud Build, you need a Dockerfile to be present along with your sources. The Hello 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

Building using Cloud Build

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

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

  2. Run the command:

    gcloud builds submit --tag IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .

For tips on improving build performance, see Speeding up your builds

Building locally and pushing using Docker

If you have Docker installed locally, you can use docker build instead of using Cloud Build or Buildpacks.

To build your container image using Docker:

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

  2. Run the command:

    docker build . --tag IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .

  3. If you have not yet configured Docker to use the Google Cloud CLI to authenticate requests to Container Registry, do so now using the command:

    gcloud auth configure-docker

    You need to do this before you can push or pull images using Docker. You only need to do it once.

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

    docker push IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .

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

Building using Google Cloud's buildpacks

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

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

Building 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

    Replace IMAGE_URL with the container image URL, for example, gcr.io/myproject/myservice.

  3. Wait for the build to complete.

Building with Google Cloud's buildpacks using the pack command line

To build using the pack command:

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

  2. If you have not yet configured Docker to use the Google Cloud CLI to authenticate requests to Container Registry, do so now using the command:

    gcloud auth configure-docker

    You need to do this before you can push or pull images using Docker. You only need to do it once.

  3. If you haven't already done so, install pack.

  4. Navigate to the folder containing your sources.

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

    pack build --publish IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .

  6. Wait for pack to finish.

For more information, read the instructions under Building an Application.

What's next