Container Registry: Qwik Start

Container Registry: Qwik Start

40 minutesFree

GSP084

Google Cloud Self-Paced Labs

Overview

Google Container Registry is a private container image registry that runs on Google Cloud's reliable, fast, and secure infrastructure.

You can access Container Registry through secure HTTPS endpoints, which allow you to push, pull, and manage images from any system, VM instance, or your own hardware. Additionally, you can use the Docker credential helper command-line tool to configure Docker to authenticate directly with Container Registry.

While Docker provides a central registry for storing public images, you might not want your images to be accessible to the world. To control access to your images, you must store your images in a private registry.

This hands-on lab demonstrates how to build a Docker image containing a basic Python web app, and push it to Google Container Registry.

Setup and Requirements

Qwiklabs setup

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This Qwiklabs hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

What you need

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time to complete the lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab.

Note: If you are using a Pixelbook, open an Incognito window to run this lab.

How to start your lab and sign in to the Google Cloud Console

  1. 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 a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

    Tip: Open the tabs in separate windows, side-by-side.

    If you see the Choose an account page, click Use Another AccountChoose an account

  3. In the Sign in page, paste the username that you copied from the Connection Details panel. Then copy and paste the password.

    Important: You must use the credentials from the Connection Details panel. Do not use your Qwiklabs credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

  4. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Cloud Console Menu

The Google Cloud Shell

Activate Cloud Shell

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. Cloud Shell provides command-line access to your Google Cloud resources.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

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:

Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list
content_copy

(Output)

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)content_copy

(Example output)

Credentialed accounts:
 - google1623327_student@qwiklabs.netcontent_copy

You can list the project ID with this command:

gcloud config list project
content_copy

(Output)

[core]
project = <project_ID>content_copy

(Example output)

[core]
project = qwiklabs-gcp-44776a13dea667a6content_copy

For full documentation of gcloud see the gcloud command-line tool overview.

Building the Docker Image

A Docker image is constructed from the instructions contained in a Dockerfile. In this lab, the Dockerfile also references other files which are the application code and its dependencies (requirements.txt).

This quickstart shows you how to build a small Python web application which uses the Flask web framework to serve a web page. The web page displays the message "Hello, World!"

To prepare the Docker image, you are going to make a Docker image directory and the three required files. There are a variety of tools available to do this, but for this lab, we'll use nano to make the files.

  1. In the Cloud Shell command line, make and then go to the Docker image directory, dimage:

mkdir dimage
content_copy
cd dimage
content_copy
  1. Open nano and make Dockerfile:

nano Dockerfile
content_copy
  1. Add content into Dockerfile to define your image environment:

# Import Python runtime and set up working directory
FROM python:3.7-slim
WORKDIR /app
ADD . /app

# Install any necessary dependencies
RUN pip3 install -r requirements.txt

# Open port 8080 for serving the webpage
EXPOSE 8080

# Run app.py when the container launches
CMD ["python3", "app.py"]
content_copy
  1. Exit nano (Ctrl+x), save Dockerfile (y) and then press Enter.

  2. Open nano and make requirements.txt, to define your image dependencies.

nano requirements.txt
content_copy
  1. Add content to requirements.txt.

Flask
content_copy
  1. Exit nano (Ctrl+x), save requirements.txt (y) and then press Enter.

  2. Open nano to make the third file, app.py. This is the Python web application which will display the message "Hello, World!".

nano app.py
content_copy
  1. Add the following content.

from flask import Flask
import os
import socket

app = Flask(__name__)

@app.route("/")
def hello():
    html = "<h3>Hello, World!</h3>"
    return html

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=8080)
content_copy
  1. Exit nano (Ctrl+x), save app.py (y) and then press Enter.

Running the Flask application

To run the Flask application you must first install dependencies.

  1. Install dependencies:

sudo pip3 install flask
content_copy
  1. Run the flask application:

python3 app.py
content_copy
  1. View the results using the Web preview feature. Click the Web preview > Preview on port 8080:

    CloudShell_web_preview.png

    A new tab will open to display your results:

    web_prev_response.png

     

  2. Before continuing, stop the running node server by typing Ctrl-c in Cloud Shell.

Building the Docker image

Use the following command to build the Docker image:

docker build -t quickstart-image .
content_copy

The image build is successful when you see these last two lines. (Your project id will be different):

Successfully built 9bb7dfec8bc2
Successfully tagged quickstart-image:latestcontent_copy

Tagging your image

Before you push your Docker image, you need to tag it with its registry name. Tagging your Docker image with a registry name configures the docker push command to push the image to a specific location.

The registry name ([REGISTRY_NAME]) format is below:

[HOSTNAME]/[PROJECT-ID]/[IMAGE]content_copy
  • [HOSTNAME] for this lab is gcr.io
  • [PROJECT-ID] is your Google Cloud project ID found in the Connection Details section of the lab.
  • [IMAGE] is your image's name. For this lab it's quickstart-image

The format of the command to tag your Docker image for Container Registry is:

docker tag [IMAGE] [REGISTRY_NAME]content_copy

For this lab use the following command to tag your Docker image, replacing [PROJECT-ID] with your Project ID:

docker tag quickstart-image gcr.io/[PROJECT-ID]/quickstart-image
content_copy

You are now ready to push your image to Container Registry!

Pushing your image

The format of the command to push your Docker image to Container Registry is:

docker push [HOSTNAME]/[PROJECT-ID]/[IMAGE]content_copy

Run the following command, this will allow Docker to access your project's Container Registry :

gcloud auth configure-docker
content_copy

Run the following command, replacing [PROJECT-ID], with your Project ID:

docker push gcr.io/[PROJECT-ID]/quickstart-image
content_copy

You can view images hosted by Container Registry via the Cloud Console, or by visiting the image's registry name in your web browser (remember to replace [PROJECT-ID] with your Project ID):

http://gcr.io/[PROJECT-ID]/quickstart-image
content_copy

In the Google Cloud console, go to the navigation menu and select Container Registry under the Tools header. You should see the quickstart image saved there:

cr.png

 

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Pushing your image.

Check my progress

Test your Understanding

Below are multiple-choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

You can tag your Docker image for Container Registry with:

docker run

gcloud docker -- push

docker pull

docker tagSubmit

Docker can build images automatically by reading the instructions from a ____.

requirements.txt

Flask

Dockerfile

Container RegistrySubmit

Testing your image

To test that an instance of your image runs as expected, you can pull your image from Container Registry and run an instance from your system.

The command format to pull your image from Container Registry is:

gcloud docker -- pull [HOSTNAME]/[PROJECT-ID]/[IMAGE]content_copy

Verify the authorization:

gcloud auth configure-docker
content_copy
  1. Use the following command to pull your image from Container Registry, replacing [PROJECT-ID] with your Project ID:

docker pull gcr.io/[PROJECT-ID]/quickstart-image
content_copy

You should see output similar to the following:

latest: Pulling from [PROJECT-ID]/[IMAGE]
  Digest: sha256:70c42...
  Status: Image is up to date for [HOSTNAME]/[PROJECT-ID]/[IMAGE]content_copy
  1. See your pulled images:

gcloud container images list
content_copy

Example output:

NAME
  [HOSTNAME]/[PROJECT-ID]/[IMAGE]content_copy
  1. Use the following command to run an instance of your image, replacing [PROJECT-ID] with your Project ID:

docker run -p 8080:8080 gcr.io/[PROJECT-ID]/quickstart-image
content_copy
  1. View the Hello World! results using the Web preview feature. Click Web preview > Preview on port 8080.

web_prev_response.png

 

Your Docker container is registered and deployed and you viewed the running app.

Congratulations!

baseline_infra_quest_icon.png

Finish Your Quest

Continue your Quest with Baseline: Infrastructure. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Take Your Next Lab

This lab is also part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you'd like to take!

Next Steps /Learn More

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated January 5, 2020

Lab Last Tested December 21, 2020

Copyright 2021 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值