Jump to Content
Developers & Practitioners

How to build and push a Docker image with Cloud Build

December 7, 2022
https://storage.googleapis.com/gweb-cloudblog-publish/images/build_2022_hv841s0.max-2500x2500.jpg
Bukola Ayodele

Developer Advocate

If you want to create a serverless service, you’ll want to check out Cloud Build

Cloud Build is a popular Google Cloud service that allows you to execute your builds on Google Cloud. Cloud Build can import source code from various repositories or cloud storage spaces, execute a build to your specifications, and produce artifacts such as Docker containers or Java archives. Cloud Build is serverless, which means it scales up and down with no infrastructure to set up, upgrade, or scale. In this blog post, we’ll learn how to build and push a Docker image with Cloud Build. If you want to follow along with this blog post, check out this tutorial.

Step 1

The first step is to create a new project, we’ll use an existing project called “test-ctd” and enable Google Cloud APIs like Cloud Build API, Artifact Registry API. If you have billing enabled and or are in your free trial mode you’ll be able to enable the necessary APIs. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/Enable_APIs_In_Google_Cloud_Project.max-2000x2000.png
Enable APIs In Google Cloud Project

Step 2

The second step is to prepare the source files. We’ll open the Cloud Shell, where we’ll be able to create a directory called “quickstart-docker” with the commands in the image below

https://storage.googleapis.com/gweb-cloudblog-publish/images/Create_Directory_Quick_Start_Docker.max-700x700.png
Create Quickstart Directory

Running this command brings us to the Cloud Shell Editor, where we’ll create a file named quickstart.sh in the quickstart-docker folder we previously created. The file will hold a small command, which you’ll see in the image below. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/quickstart_sh_file.max-1700x1700.png
Creating Quickstart.sh File

Make sure to save the file, by clicking File > Save in the top navigation bar.

https://storage.googleapis.com/gweb-cloudblog-publish/images/How_To_Save.max-2000x2000.png
How To Save Quickstart.sh File

Next we’ll create the Dockerfile, which Cloud build will execute. In the dockerfile we’ll add a small script to run quickstart.sh. Make sure to save this file as well. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/dockerfile_.max-1700x1700.png
Create Dockerfile

We’ll return to the terminal where we’ll run the command in the image below, to make quickstart.sh executable. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/Executable.max-1200x1200.png
Command To Quickstart.sh Executable

Step 3

We’ll move on to the third step, which is to create a new Docker repository in the repository in Artifact Registry.


We’ll start by creating a new Docker repo named quickstart-docker-repo in the location us-west2 with the description "Docker repository". The command to run this action is depicted below

https://storage.googleapis.com/gweb-cloudblog-publish/images/Screen_Shot_2022-12-06_at_11.06.36_AM.max-1100x1100.png
Add File To Artifact Registry

Next we’ll verify that the repository was created by running the command below. After running the command we see quickstart-docker repo is in the Artifact Registry. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/Artifact_Registry_CROPPED_1.max-1000x1000.png
Check Files in Artifact Registry

Step 4

In the same directory that contains quickstart.sh and the Dockerfile, create a file named cloudbuild.yaml with the following contents

Loading...

https://storage.googleapis.com/gweb-cloudblog-publish/images/image7_UUPrG81.max-1100x1100.jpg

Next we’ll start the build by running the command below. With this command we built a quickstart-image using a build config file and pushed the image to Artifact Registry.

Loading...

Step 5

Next we’ll navigate to Cloud Build > History to see the build we executed. If you click on the particular build you’ll be able to see the details of the build summary like the build log, execution details, and build artifacts. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/BUILD_HISTORY.max-2000x2000.png
Build History

Step 6

For the final step, we will delete the resources created to prevent incurring charges. Go back to the navigation bar and visit Artifact Registry Next we’ll select our recently created quickstart-docker-repo and click DELETE.

https://storage.googleapis.com/gweb-cloudblog-publish/images/DELETE_REPO.max-2000x2000.png
Delete Resource

In Conclusion

There you have it! Now you’ve learned how easy it is to build and push a Docker image with Cloud Build. If you’d like to follow another Google Cloud tutorial visit this blog post

Posted in