Google Cloud: Building Containers with Dockerfile and Cloud Build

Share At:

Introduction

Cloud Build is a service that executes your builds on Google Cloud Platform’s infrastructure. Cloud Build can import source code from a variety of repositories or cloud storage spaces, execute a build to your specifications, and produce artifacts such as Docker containers or Java archives.

Building Containers with DockerFile and Cloud Build

We can write build configuration files to provide instructions to Cloud Build as to which tasks to perform when building a container. These build files can fetch dependencies, run unit tests, analyses and more.

In this task, we will create a DockerFile and use it as a build configuration script with Cloud Build. We will also create a simple shell script (quickstart.sh) which will represent an application inside the container.

  1. Ensure that below API’s have been enabled:

Cloud Build

Container Registry

In the Google Cloud Console, on the Navigation menu > click APIs & Services. Click Enable APIs and Services. In the Search for APIs & Services BoxType the above mentioned API’sIf Disabled, Enable The above two API’s.

2. On the Google Cloud Console title bar, click Activate Cloud Shell. When prompted, click Continue.

3. Create aquickstart.sh file using the your favorite text editor.

sudo vi quickstart.sh

4. The quickstart.sh file should look like below:

5. Also Create a “Dockerfile” file using a text editor. The Dockerfile should look like below:

6. The Instructions in the Dockerfile has below meaning:

7. In Cloud Shell, run the following command to make the quickstart.sh script executable.

sudo chmod +x quickstart.sh

8. In Cloud Shell, run the following command to build the Docker container image in Cloud Build:

gcloud builds submit — tag gcr.io/${GOOGLE_CLOUD_PROJECT}/quickstart-image .

Don’t miss the dot (“.”) at the end of the command. The dot specifies that the source code is in the current working directory at build time.

When the build completes, your Docker image is built and pushed to Container Registry.

Validating Newly Created Images in the Container Registry and Downloading Docker Image.

  1. In the Google Cloud Console, on the Navigation menu , click Container Registry > Images. The quickstart-image Docker image appears in the list.

2. At this stage, Running “Docker Images” Command will not show any Image on the system.

3. Use “docker pull” command to download the image.

docker pull gcr.io/qwiklabs-gcp-02–0cd3114364e3/quickstart-image

4. Validate that the Image has been downloaded to your machine and is ready for use.

This Concludes our tutorial for Building Containers with Dockerfile and Cloud Build.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us