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

[email protected]:~/helloDocker (sneppets-gcp)$ ls
Dockerfile  
helloDockerQuickStart.sh

1. Create file named helloDockerQuickStart.sh with the following content

#!/bin/sh
echo "Hello, World!"

2. Then create Dockerfile file as shown below

FROM alpine
COPY helloDockerQuickStart.sh /
CMD ["/helloDockerQuickStart.sh"]

3. Make helloDockerQuickStart.sh executable

$ sudo chmod +x helloDockerQuickStart.sh

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

$ gcloud builds submit --tag gcr.io/sneppets-gcp/hellodocker-image .

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 [https://cloudbuild.googleapis.com/v1/projects/sneppets-gcp/builds/0972027c-b206-48e9-8398-f0093a33fcdb].
Logs are available at [https://console.cloud.google.com/gcr/builds/0972027c-b206-48e9-8398-f0093a33fcdb?project=941515528061].
----------------------------------------------------------------- REMOTE BUILD OUTPUT -----------------------------------------------------------------
starting build "0972027c-b206-48e9-8398-f0093a33fcdb"
-----------------------
-----------------------
DONE
-------------------------------------------------------------------------------------------------------------------------------------------------------

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
gcr.io/sneppets-gcp/hellodocker-image (+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

cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/hellodocker-image', '.' ]
images:
- 'gcr.io/$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 gcr.io/sneppets-gcp/hellodocker-image

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

Hello World!

Further Learning

References

Leave a Reply

avatar
  Subscribe  
Notify of