Build - Google Cloud
Build and push a Docker image with Cloud Build
Learn how to get started with Cloud Build by building a Docker image and pushing the image to Artifact Registry. Artifact Registry provides a single location for managing private packages and Docker container images.
You will first build the image using a Dockerfile
, which is the Docker configuration file, and then build the same image using the Cloud Build configuration file.
Before you begin
- If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs.
- Install the Google Cloud CLI.
To initialize the gcloud CLI, run the following command:
Prepare source files to build
You'll need some sample source code to package into a container image. In this section, you'll create a simple shell script and a Dockerfile
. A Dockerfile
is a text document that contains instructions for Docker to build an image.
Open a terminal window.
Create a new directory named
quickstart-docker
and navigate into it:Create a file named
quickstart.sh
with the following contents:Create a file named
Dockerfile
with the following contents:In the terminal window, run the following command to make
quickstart.sh
executable:
Create a Docker repository in Artifact Registry
Create a new Docker repository named
quickstart-docker-repo
in the locationus-west2
with the description "Docker repository":Verify that your repository was created:
You will see
quickstart-docker-repo
in the list of displayed repositories.
Build an image using Dockerfile
Cloud Build allows you to build a Docker image using a Dockerfile
. You don't require a separate Cloud Build config file.
To build using a Dockerfile
:
Get your Google Cloud project ID by running the following command:
Run the following command from the directory containing
quickstart.sh
andDockerfile
:
After the build is complete, you will see an output similar to the following:
You've just built a Docker image named quickstart-image
using a Dockerfile
and pushed the image to Artifact Registry.
Build an image using a build config file
In this section you will use a Cloud Build config file to build the same Docker image as above. The build config file instructs Cloud Build to perform tasks based on your specifications.
In the same directory that contains
quickstart.sh
and theDockerfile
, create a file namedcloudbuild.yaml
with the following contents. This file is your build config file. At build time, Cloud Build automatically replaces$PROJECT_ID
with your project ID.Start the build by running the following command:
When the build is complete, you will see an output similar to the following:
You've just built quickstart-image
using the build config file and pushed the image to Artifact Registry.
View build details
Open the Cloud Build page in the Google Cloud console.
Select your project and click Open.
You will see the Build history page:
In the Region drop-down menu, select
us-west2
to view builds in that region.Click on a particular build.
You will see the Build details page.
To view the artifacts of your build, under Build Summary, click Build Artifacts.
You will see an output similar to the following:
You can download your build log and view your image details in Artifact Registry from this page.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Open the Artifact Registry page in the Google Cloud console.
Select your project and click Open.
Select quickstart-docker-repo.
Click Delete.
You have now deleted the repository that you created as part of this quickstart.
Comments
Post a Comment