build docker image dockerfile cloud build

Build a Docker Image with a Dockerfile and Cloud Build in GCP?

This tutorial explains you how to build a Docker image with a Dockerfile and Cloud Build (named as leader for Continuous Integration) in Google Cloud Platform.

First you should enable Cloud Build API and then install and initialize Cloud SDK.  Note, if you are already using Google Compute Engine instances in your account, then by default Cloud SDK is installed.

What is Cloud Build  and Cloud SDK?

Cloud Build is Google’s Continuous Integration(CI) and Continuous Delivery(CD) platform.

Cloud SDK has set of tools using which you can manage resources and application hosted on GCP. This SDK provides the following command line tools for you, gcloud (to manage GCP resources and developer workflow), gsutil (Google Cloud Storage util) and bq (BigQuery util).

Build Docker Image with Dockerfile

First create the following source files in your project directory

sneppets@cloudshell:~/helloDocker (sneppets-gcp)$ ls

1. Create file named with the following content

echo "Hello, World!"

2. Then create Dockerfile file as shown below

FROM alpine
CMD ["/"]

3. Make executable

$ sudo chmod +x

4. Now build a Docker image using Dockerfile and push the image to Container Registry ( using gcloud command.

$ gcloud builds submit --tag .

Creating temporary tarball archive of 3 file(s) totalling 303 bytes before compression.
Uploading tarball of [.] to [gs://sneppets-gcp_cloudbuild/source/1574270987.1-21736be605a0496f93603365a05c6a4b.tgz]
Created [].
Logs are available at [].
----------------------------------------------------------------- REMOTE BUILD OUTPUT -----------------------------------------------------------------
starting build "0972027c-b206-48e9-8398-f0093a33fcdb"

ID                                    CREATE_TIME                DURATION  SOURCE
0972027c-b206-48e9-8398-f0093a33fcdb  2019-11-20T17:29:49+00:00  13S       gs://sneppets-gcp_cloudbuild/source/1574270987.1-21736be605a0496f93603365a05c6a4b.tgz
IMAGES                                           STATUS (+1 more)  SUCCESS

Build Docker Image using Cloud Build

Now you are going to build same Docker image using a build config file (cloudbuild.yaml), which will provide instructions to Google Cloud Build what tasks it should perform. Below the sample build config file


- name: ''
  args: [ 'build', '-t', '$PROJECT_ID/hellodocker-image', '.' ]
- '$PROJECT_ID/hellodocker-image'

Then issue the following build command.

$ gcloud builds submit --config cloudbuild.yaml .

You can verify docker images via Google Cloud Console (GCP Console -> Container Registry -> Images) also as shown below.

docker image dockerfile cloud build

Run the Docker Image

You can verify the image that you had built by issuing the following command in the terminal.

$ docker run

Unable to find image '' locally
latest: Pulling from sneppets-gcp/hellodocker-image
89d9c30c1d48: Pull complete
ee9838ab953f: Pull complete
Digest: sha256:a821db41717ec1e7e77cade685bec54b29219dd126b77cd08b41bec939d8d497
Status: Downloaded newer image for

Hello World!

Further Learning


Notify of

Inline Feedbacks
View all comments