Build container images

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 Dockerfile handy.
  • Have a Docker repository for storing images in Artifact Registry, or create a new repository.
  • If you want to use the gcloud commands 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:

  1. In the same directory that contains your application source code, create a file named cloudbuild.yaml or cloudbuild.json.
  2. In the build config file:

    • Add a name field and specify the prebuilt Docker image. The prebuilt image is stored at gcr.io/cloud-builders/docker. In the following sample config file, the name field specifies that the prebuilt Docker image is used by Cloud Build to execute the task indicated by the args field.
    • In the args field, 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 Dockerfile and source code are in different directories, add -f and the path to the Dockerfile to the list of arguments in the args field:

      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 your Dockerfile.
  3. Start the build using the build config file:

    gcloud builds submit --config CONFIG_FILE_PATH SOURCE_DIRECTORY
    

    Where:

    • 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_PATH and SOURCE_DIRECTORY in the gcloud builds submit command, 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.