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 Google Cloud's buildpacks
Before you begin
You need the Google Cloud CLI to run some of the commands in this page.
Create a repository at a supported container registry. To create an Artifact Registry repository, run:
gcloud artifacts repositories create REPOSITORY \ --repository-format=docker \ --location=LOCATION \ --description="DESCRIPTION" \ --immutable-tags \ --async
You can configure Docker to get access to Artifact Registry using the gcloud CLI credential helper
gcloud auth configure-docker LOCATION-docker.pkg.dev
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:
Navigate to the folder containing your sources and Dockerfile.
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 shapeLOCATION-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 Google Cloud's buildpacks.
To build your container image using Docker:
Navigate to the folder containing your sources and
Dockerfile
.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 shapeLOCATION-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/amd64
in the command line.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 shapeLOCATION-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:
Navigate to the folder containing your sources.
Run the command:
gcloud builds submit --pack image=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 shapeLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.Wait for the build to complete.
Building with Google Cloud's buildpacks using the pack
command line
To build using the pack command:
If you haven't already done so, install Docker.
If you haven't already done so, install
pack
.Navigate to the folder containing your sources.
Run the following command to build and push to your supported 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 shapeLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.Wait for
pack
to finish.
For more information, read the instructions under Building an Application.
What's next
- After your container has been built, you can test locally before deploying to Cloud Run; see Testing a Cloud Run service locally to learn more.
- To learn more about the contract your containers must respect to be deployed to Cloud Run, see Container contract.
- To deploy your built containers to Cloud Run, follow Deploying services.
- To automate the builds and deployments of your Cloud Run services using Cloud Build Triggers, set up continuous deployment.
- To perform optimal container builds for Java application, see Building Java containers with Jib.