Working with GCP Cloud Build

Cloud Guru
3 min readDec 29, 2023

Use GCP Cloud Build to build and push containers.

Containerization has become the focal point in the evolution of deploying and managing code. Implementing containerization has inspired organizations to maximize managed cloud infrastructures like Cloud Build to speedily build, test and deploy container images.

About Containers and their benefits

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. (docker website, 2019)

Containers are lightweight because they don’t carry a full operating system. They can be created and shut down very quickly because you’re just starting and stopping the processes that make up the application and not booting up an entire VM and initializing an operating system for each application. You develop application code in the usual way, on desktops, laptops, and servers. The container allows you to execute your final code on VMs without worrying about software dependencies like application run times, system tools, system libraries, and other settings. You package your code with all the dependencies it needs, and the engine that executes your container, is responsible for making them available at runtime. Gone are the days when Developers and System Engineers or Server Administrators used to argue and blame each other for the failure of software deployment projects. Containers now allow developers to safely make assumptions about the underlying hardware and software.

About GCP Cloud Build

Cloud Build is a managed service on Google Cloud Platform infrastructure that allows you to continuously build, test and deploy containers. Cloud Build lets you build software quickly across all languages. Cloud Build works across multiple environments such as VMs, serverless, Kubernetes, or Firebase.

This article explains how to use Cloud Build to build a Docker image and push the image to a Container Registry.

Getting Started

Prerequisites

  1. You need a GCP account. You can sign up for a free $300 account.
  2. Create a GCP project.

Step 1: Make sure needed APIs are enabled

In the GCP Console, go to APIs and Services

  • Check the Cloud Build API
  • Check the container-registry API

If the APIs are not enabled, click Enable API to enable them.

Step 2: Create a Dockerfile

  • Launch Cloud Shell. You can do this locally but you will need to install Google Cloud SDK
  • Create a simple Dockerfile in the present directory.
FROM alpine
COPY quickstart.sh /
CMD ["/quickstart.sh"]

nano quickstart.sh

#!/bin/sh
echo "Working with GCP Cloud Build"
echo "The time is $(date)."

set the permissions of the container so that it is executable

chmod +x quickstart.sh

Step 3: Build the Docker container image in Cloud Build

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 .

You could also build containers with a custom configuration file.

Step 4: Building Containers with a build configuration file and Cloud Build

Create the config file: nano cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'

Start a Cloud Build using cloudbuild.yaml as the build configuration file:

gcloud builds submit — config cloudbuild.yaml .

Working with Cloud Build to build Docker images

Once the builds succeeded, go look at it in Google Container Registry.

Container Registry

The video tutorial for this demonstration is available on Cloud Guru Youtube Channel. Click here to watch the video, like, and subscribe to the channel

Thanks!

--

--

Cloud Guru

Join us to follow the latest news & announcements around Cloud, DevOps, Artificial intelligence, Machine learning, Internet of things and Big data & analytics.