Quickstart: Build

This page explains how to use Cloud Build to build a Docker image and push the image to Container Registry. Container Registry provides secure private Docker image storage on Google Cloud.

You will first build the image using a Dockerfile, which is the Docker configuration file, and then build the same image using the Cloud Build configuration file.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. Enable the Cloud Build API.

    Enable the API

  5. Install and initialize the Cloud SDK.

Preparing source files

You'll need some sample source code to package into a container image. In this section, you'll create a simple shell script and a Dockerfile. A Dockerfile is a text document that contains instructions for Docker to build an image.

  1. Open a terminal window.

  2. Create a new directory named quickstart-docker and navigate into it:

    mkdir quickstart-docker
    cd quickstart-docker
    
  3. Create a file named quickstart.sh with the following contents:

    #!/bin/sh
    echo "Hello, world! The time is $(date)."
    
  4. Create a file named Dockerfile with the following contents:

    FROM alpine
    COPY quickstart.sh /
    CMD ["/quickstart.sh"]
    
  5. Run the following command to make quickstart.sh executable:

    chmod +x quickstart.sh
    

Build using Dockerfile

Cloud Build allows you to build a Docker image using a Dockerfile. You don't require a separate Cloud Build config file.

To build using a Dockerfile:

  1. Get your Cloud project ID by running the following command:

    gcloud config get-value project
    
  2. Run the following command from the directory containing quickstart.sh and Dockerfile, where project-id is your Cloud project ID:

    gcloud builds submit --tag gcr.io/project-id/quickstart-image
    

After the build is complete, you will see an output similar to the following:

DONE
-------------------------------------------------------------------------------------------------------------------------
ID                                    CREATE_TIME                DURATION SOURCE                                   STATUS
$BUILD_ID                             2016-09-28T13:46:29+00:00  9S    gs://[PROJECT_ID]_cloudbuild/source/1508159187.8-b0d8841d51674a30aebd1e55bb99486f.gz  gcr.io/[PROJECT_ID]/quickstart-image (+1 more)       SUCCESS

You've just built a Docker image named quickstart-image using a Dockerfile and pushed the image to Container Registry.

Build using a build config file

In this section you will use a Cloud Build config file to build the same Docker image as above. The build config file instructs Cloud Build to perform tasks based on your specifications.

  1. In the same directory that contains quickstart.sh and the Dockerfile, create a file named cloudbuild.yaml with the following contents. This file is your build config file. At build time, Cloud Build automatically replaces $PROJECT_ID with your project ID.

    steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
    images:
    - 'gcr.io/$PROJECT_ID/quickstart-image'
    
  2. Start the build by running the following command:

    gcloud builds submit --config cloudbuild.yaml
    

When the build is complete, you will see an output similar to the following:

DONE
-------------------------------------------------------------------------------------------------------------------------
ID                                    CREATE_TIME                DURATION SOURCE                                   STATUS
$BUILD_ID                             2016-09-28T13:46:29+00:00  8S    gs://[PROJECT_ID]_cloudbuild/source/1508158566.55-725755714baa4b7e9e99984c422ec4e2.gz  gcr.io/[PROJECT-ID]/quickstart-image (+1 more)       SUCCESS

You've just built quickstart-image using the build config file and pushed the image to Container Registry.

View the build details

  1. Open the Cloud Build page in the Google Cloud Console.

    Open the Cloud Build page

  2. Select your project and click Open.

    You will see the Build history page:

    Screenshot of the build history page

  3. Click on a particular build.

    You will see the Build details page.

  4. To view the artifacts of your build, under Build Summary, click Build Artifacts.

    You will see an output similar to the following:

    Screenshot of build artifacts

    You can download your build log and view your image details in Container Registry from this page.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.

  1. Open the Container Registry page in the Google Cloud Console.

    Open the Container Registry page

  2. Select your project and click Open.

  3. Click quickstart-image.

  4. Select all the images and click Delete.

You have now deleted the images that you created as part of this quickstart.

That's it, you completed this quickstart!

What's next