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

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, gcr.io/myproject/my-image:latest.

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.

Before building your sources into a container image ("containerizing") using Docker, you need a Dockerfile to be present along your sources. The Build and deploy quickstart contains sample applications and Dockerfiles in many popular languages.

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, gcr.io/myproject/my-image:latest.

  3. If you have not yet configured Docker to use the gcloud command-line tool 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 Container Registry:

    docker push IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, gcr.io/myproject/my-image:latest.

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

Building using Buildpacks

For complete information on Buildpacks, see the Buildpacks website. Google provides a set of CNCF-compatible Buildpacks that build source code into container images designed to run on Google Cloud container platforms, including Cloud Run.

These platforms are currently supported:

  • Go
  • Node.js
  • Python
  • Java
  • .NET Core

Building with Buildpacks using Cloud Build

To build with a Buildpack:

  1. Navigate to the folder containing your sources.

  2. Run the command:

    gcloud alpha 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 Buildpack 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 gcloud command-line tool 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, gcr.io/myproject/my-image:latest.

  6. Wait for pack to finish.

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

What's next