
始める前に
- ラボでは、Google Cloud プロジェクトとリソースを一定の時間利用します
- ラボには時間制限があり、一時停止機能はありません。ラボを終了した場合は、最初からやり直す必要があります。
- 画面左上の [ラボを開始] をクリックして開始します
Build a container image
/ 40
Modify the container and rebuild a container image
/ 20
Publishing a container image
/ 40
Docker is an open platform for developing, shipping, and running applications in containers. Docker helps you build, test, and deploy code faster, and it shortens the cycle between developing and running code. Docker does this by combining kernel containerization features with workflows and tooling that helps you manage and deploy your applications.
Docker lets you express the application build process by using a script, called a Dockerfile. Dockerfiles provide a low-level approach that offers flexibility at the cost of complexity. The Dockerfile is a manifest that details how to turn your source code into a container image.
Docker containers can be directly used in Cloud Run and Kubernetes, which allows them to be run on these platforms with ease. After learning the essentials of Docker, you will have the skill set to start developing containerized applications.
In this lab, you learn how to:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
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:
You execute the shell commands in this lab in a separate VM that has been pre-provisioned for the lab.
To start an SSH session on the VM, in Cloud Shell, execute the following command:
When prompted, type Y to continue.
For the passphrase, type Enter to not use any.
Type Enter again.
Grant permissions to the student user on the socket used by Docker:
To set your Project ID and region environment variables, run the following commands:
In this task, you create a Docker container image by using a Dockerfile.
Create a test directory and change to it:
Create the Dockerfile
:
This is specified with the FROM instruction and the official Docker image for Node version lts (long term support).
The WORKDIR instruction sets the working directory in the container for any additional instructions that follow in the Dockerfile. For this lab, we use the /app directory as the container's working directory.
The COPY instruction copies directories or files from the source location to the destination path of the container image file system. Here, we copy files from the current directory to /app.
The EXPOSE instruction exposes the container's port so that it accepts connections on that port, which in this lab is port 80.
Finally, the CMD instruction provides the node command to execute the application in the running container.
Next, you write the code for your Node.js application. This application is a simple HTTP server that listens for requests on port 80, and responds with a static message.
Create the app.js file with your application source code:
Verify the contents of the application source file:
In this subtask, you build the container image from the Dockerfile by using the docker build
command.
Run the docker build
command:
A partial command output is below:
View the list of images that were built:
To verify the objective, click Check my progress.
After the container image is successfully built, you can run your application and test it to ensure that it behaves as expected.
To run the container, execute the command:
To view a list of running containers, execute the command:
To test the application, send it a HTTP request by using the curl
command:
Verify the application response:
Stop the container by executing the docker stop
command:
Replace [CONTAINER ID] with the value of the CONTAINER ID from the output of the command that was executed in the previous subtask.
You can have multiple versions of your containerized applications that share common layers in the image. In this task, you create another version of the containerized application and test both versions.
In the test
directory, update the content of the app.js
file:
Verify the contents of the modified application source file with the cat
command:
Rebuild the image with a new tag:
Verify the output of the docker build
command:
As seen in the output, in the new container image only layers from Step 3 and below are modified because changes were made in the app.js source file.
To run the new container, execute the command:
To view a list of running containers, execute the command:
To test the application, send it a HTTP request by using curl
:
Verify the application response:
Click Check my progress to verify the objective.
There are some easy techniques to troubleshoot your containerized applications. We review some of these methods in this task.
First, retrieve the ID of the container whose logs you need to view:
To view the container logs, run the docker logs
command:
Replace the CONTAINER ID with the initial characters of the container ID that uniquely identifies it from the output of the previous command. To tail the log's output as the container is running, use the -f option with the logs command.
You can start an interactive shell inside a running container to troubleshoot it.
To start an interactive Bash
session, run:
From within this shell, to troubleshoot issues, you can inspect the container file system and other data files that your application might use.
Exit the Bash
shell:
To view a container's metadata, run:
Here's a partial output from the command:
Artifact Registry is a Google Cloud service that is used to store and manage software artifacts in private repositories, including container images, and software packages.
In this task, you push your container images to Artifact Registry making them available for deployment to other environments such as staging, and production, which make up your software delivery lifecycle.
Before you can push any container images to Artifact Registry, you must first create a repository.
In the Google Cloud console, in the Navigation menu (), click View all products. Then, under CI/CD Categories, navigate to Artifact Registry > Repositories.
In the Create repository page, provide the following information, and leave the remaining settings as their defaults:
Property |
Value |
Name |
my-repo |
Format |
Docker |
Location type |
Region |
Region |
|
Click Create.
Before you can push or pull images to or from the repository, you must configure Docker to authenticate requests to the repository in Artifact Registry.
To set up authentication to Docker repositories in the region
When prompted, type Y.
The full name of the repository that you created is:
To push a container image to your private registry hosted by Artifact Registry, you need to first tag the image with the repository name:
List the Docker images that you built:
To push the image to Artifact Registry, run the following command:
The output of this command is similar to:
After the command completes, in the Google Cloud console, in the Navigation menu (), click View all products. Then, under the CI/CD Categories, navigate to Artifact Registry > Repositories.
Click the my-repo repository to display the my-app Docker container image.
Click Check my progress to verify the objective.
In this task, you start with a fresh environment and then pull the container image from Artifact Registry to test it. To simulate another environment, you stop and remove all containers and images from your shell environment that were created in the previous tasks in this lab.
To stop all running containers, execute the following command:
To remove all containers, execute the following command:
To remove the registry-tagged container image, execute the following command:
To remove all other images, execute the following command:
Verify that no container images exist in your VM environment:
You should now have a fresh host environment without any local images.
Test the image by pulling it from Artifact Registry.
To pull the image from Artifact Registry, execute the following command:
To list the image, execute the following command:
To run the container, execute the following command:
To test the application, run:
Congratulations on completing this lab on the fundamentals of creating containers with Docker. In this lab, you:
For more information about Docker, and Artifact Registry, view the documentation:
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.
このコンテンツは現在ご利用いただけません
利用可能になりましたら、メールでお知らせいたします
ありがとうございます。
利用可能になりましたら、メールでご連絡いたします
1 回に 1 つのラボ
既存のラボをすべて終了して、このラボを開始することを確認してください