This page describes how to configure Cloud Build to build and store Docker images. If you're new to Cloud Build, read the quickstarts and the build configuration overview first.
Cloud Build provides pre-built images that you can reference in a Cloud Build config file to execute your tasks. These images are supported and maintained by Google Cloud. You can use the supported, prebuilt Docker image to execute Docker commands and build Docker images.
Before you begin
The instructions on this page assume that you are familiar with Docker. In addition:
- Have your application source code along with
Dockerfilehandy. - Have a Docker repository for storing images in Artifact Registry, or create a new repository.
- If you want to use the
gcloudcommands in this page, install the Google Cloud CLI. - If you want to run the images, install Docker
- If you want to sign the images with cosign, follow the instructions in Authorize service-to-service access to create a user-specified service account and grant the permissions required to generate ID tokens.
Build with a build config file
To build your Docker image using a build config file:
- In the same directory that contains your application source code,
create a file named
cloudbuild.yamlorcloudbuild.json. In the build config file:
- Add a
namefield and specify the prebuilt Docker image. The prebuilt image is stored atgcr.io/cloud-builders/docker. In the following sample config file, thenamefield specifies that the prebuilt Docker image is used by Cloud Build to execute the task indicated by theargsfield. In the
argsfield, add the arguments to build the image.YAML
steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_NAME', '.' ]JSON
{ "steps": [ { "name": "gcr.io/cloud-builders/docker", "args": [ "build", "-t", "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_NAME", "." ] } ] }
Where:
LOCATION: the regional or multi-regional location of your Docker repository in Artifact Registry.PROJECT_ID: your Google Cloud project ID.REPOSITORY: the name of your Docker repository in Artifact Registry.IMAGE_NAME: the name of your container image.If your
Dockerfileand source code are in different directories, add-fand the path to theDockerfileto the list of arguments in theargsfield:YAML
steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_NAME', '-f', 'DOCKERFILE_PATH', '.' ]JSON
{ "steps": [ { "name": "gcr.io/cloud-builders/docker", "args": [ "build", "-t", "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_NAME", '-f', 'DOCKERFILE_PATH', "." ] } ] }Where:
LOCATION: the regional or multi-regional location for your repository.PROJECT_ID: your Google Cloud project ID.REPOSITORY: the name of your Artifact Registry repository.IMAGE_NAME: the name of your container image.DOCKERFILE_PATH: path to yourDockerfile.
- Add a
Start the build using the build config file:
gcloud builds submit --config CONFIG_FILE_PATH SOURCE_DIRECTORYWhere:
CONFIG_FILE_PATH: the path to the build config file.SOURCE_DIRECTORY: the path or URL to the source code.
If you don't specify a
CONFIG_FILE_PATHandSOURCE_DIRECTORYin thegcloud builds submitcommand, Cloud Build assumes that the config file and the source code are in the current working directory.
Build with a Dockerfile
Cloud Build lets you build a Docker image using just a
Dockerfile. You don't require a separate build config file.
To build using a Dockerfile, run the following command from the directory
containing your source code and the Dockerfile:
gcloud builds submit --tag LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_NAME
Where:
LOCATION: the regional or multi-regional location for your repository.PROJECT_ID: your Google Cloud project ID.REPOSITORY: the name of your Artifact Registry repository.