
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Build two Container images in Cloud Build
/ 50
Build and Test Containers with a build configuration file and Cloud Build
/ 50
In this lab you will build a Docker container image from provided code and a Dockerfile using Cloud Build. You will then upload the container to the Artifact Registry.
In this lab, you learn how to perform the following tasks:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details panel.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
After you complete the initial sign-in steps, the project dashboard opens.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
Make a note of the name of your Google Cloud project. This value is shown in the top bar of the Google Cloud console. It will be of the form qwiklabs-gcp-
followed by hexadecimal numbers.
In the Google Cloud console, in the Navigation menu(), click APIs & Services.
Click Library.
In the Search for APIs & Services box, type Cloud Build.
In the resulting card for the Cloud Build API, if you do not see a message confirming that the Cloud Build API is enabled, click the Enable button.
Use the Back button to return to the previous screen with a search box. In the search box, type Artifact Registry.
In the resulting card for the Google Artifact Registry API, if you do not see a message confirming that the Artifact Registry API is enabled, click the Enable button.
You 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, you'll create a DockerFile and use it as a build configuration script with Cloud Build. You will also create a simple shell script (quickstart.sh) which will represent an application inside the container.
On the Google Cloud console title bar, click Activate Cloud Shell.
When prompted, click Authorize.
Cloud Shell opens at the bottom of the Google Cloud console window.
quickstart.sh
file using the nano text editor:quickstart.sh
file:Save the file and close nano by pressing the CTRL+X keys, then press Y and ENTER.
Create an empty Dockerfile
file using the nano text editor:
This instructs the build to use the Alpine Linux base image.
This adds the quickstart.sh
script to the / directory in the image.
This configures the image to execute the /quickstart.sh
script when the associated container is created and run.
The Dockerfile should now look like this:
Save the file and close nano by pressing the CTRL+X keys, then press Y and ENTER.
In Cloud Shell, run the following command to make the quickstart.sh
script executable:
quickstart-docker-repo
in the location When the build completes, your Docker image is built and pushed to the Artifact Registry.
In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Artifact Registry.
Click the repository named quickstart-docker-repo
.
The quickstart-image
Docker image appears in the list.
Cloud Build also supports custom build configuration files. In this task you will incorporate an existing Docker container using a custom YAML-formatted build file with Cloud Build.
Let's create a sample custom cloud build configuration file called cloudbuild.yaml
.
cloudbuild.yaml
with nano using the following command:cloudbuild.yaml
file:Press Ctrl+O, and then press Enter to save your edited file.
Press Ctrl+X to exit the nano text editor.
Run the below command to set our region variable and insert that value into the yaml file.
cloudbuild.yaml
:You will see the following:
This file instructs Cloud Build to use Docker to build an image using the Dockerfile specification in the current local directory, tag it with gcr.io/$PROJECT_ID/quickstart-image
($PROJECT_ID
is a substitution variable automatically populated by Cloud Build with the project ID of the associated project), and then push that image to Artifact Registry.
cloudbuild.yaml
as the build configuration file:The build output to Cloud Shell should be the same as before. When the build completes, a new version of the same image is pushed to Artifact Registry.
In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Artifact Registry.
In search results, click Artifact Registry.
Click the repository named quickstart-docker-repo > quickstart-image.
Two versions of quickstart-image
are now in the list.
Click Check my progress to verify the objective.
In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Cloud Build.
In search results, click Cloud Build.
In Cloud Build, click History. Two builds appear in the list.
Click the build ID for the build at the top of the list. The details of the build, including the build log, are displayed.
The true power of custom build configuration files is their ability to perform other actions, in parallel or in sequence, in addition to simply building containers: running tests on your newly built containers, pushing them to various destinations, and even deploying them to Kubernetes Engine.
In this task, we will see a simple example, a build configuration file that tests the container it built and reports
the result to its calling environment.
The first step is to alter the quickstart.sh
file.
In Cloud Shell, open quickstart.sh
in nano.
Replace the existing with the following:
Press Ctrl+O, and then press Enter to save your edited file.
Press Ctrl+X to exit the nano text editor.
Let's create a new custom cloud build configuration file called cloudbuild2.yaml
. This has been slightly modified to demonstrate Cloud Build's ability to test the containers it has built.
cloudbuild2.yaml
with nano using the following command:cloudbuild2.yaml
file:Press Ctrl+O, and then press Enter to save your edited file.
Press Ctrl+X to exit the nano text editor.
Run the below command to insert our region value into the yaml file.
cloudbuild2.yaml
:You will see the following:
In addition to its previous actions, this build configuration file runs the quickstart-image
it has created. In this task, the quickstart.sh
script has been modified so that it simulates a test failure when an argument ['fail']
is passed to it.
cloudbuild.yaml
as the build configuration file:You will see output from the command that ends with text like this:
Output
The command will reply with a non-zero value. If you had embedded this build in a script, your script would be able to act up on the build's failure.
Click Check my progress to verify the objective.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Ta treść jest obecnie niedostępna
Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem
Świetnie
Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem
One lab at a time
Confirm to end all existing labs and start this one