Quickstart for Docker

This page explains how to use Google Cloud Container Builder to build a Docker image and push the image to Container Registry. You will first build the image using a Dockerfile and then build the same image using the Container Builder 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 Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Container Builder API.

    Enable the API

  5. Install and initialize the Cloud SDK.

Log in to Google Cloud

  1. Authorize the gcloud command-line tool to access your project:

    gcloud auth login
    
  2. Configure your project for the gcloud tool, where [PROJECT_ID] is your Cloud Platform project ID that you created or selected in the previous section:

    gcloud config set project [PROJECT_ID]
    

Build using Dockerfile

Container Builder allows you to to build a Docker image using a Dockerfile; you don't require a Container Builder configuration file to build a Docker image if you have the Dockerfile.

To build using a Dockerfile:

  1. Create a file named quickstart.sh with the following contents:

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

    FROM alpine
    COPY quickstart.sh /
    CMD ["/quickstart.sh"]
    
  3. Run the following command from the directory containing quickstart.sh and Dockerfile, where [PROJECT_ID] is your Cloud Platform project ID:

    gcloud container 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 request

In this section you will use a build request, which is a file named cloudbuild.yaml, to build quickstart-image. A build request is a configuration file that instructs Container Builder to perform tasks.

  1. Create a file named cloudbuild.yaml with the following contents. At build time, Container Builder 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 container 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 a build request and pushed the image to Container Registry.

View the build details

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

    Open the Container Registry page

  2. Select your project and click Open.

  3. In the left nav, click Build history.

You will see a page similar to the following screenshot:

Screenshot of the build history page

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Open the Container Registry page in the Google Cloud Platform 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.

The images that you created as part of this quickstart are deleted from your project.

That's it, you completed this quickstart!

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Cloud Container Builder