Google Cloud: Introduction to Container Registry

Share At:

Push Docker containers to Google Container Registry (GCR) | Buddy: The  DevOps Automation Platform

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.

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
cd dimage
  1. Open nano and make Dockerfile:
nano Dockerfile
  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"]
  1. Exit nano (Ctrl+x), save Dockerfile (y) and then press Enter.
  1. Open nano and make requirements.txt, to define your image dependencies.
nano requirements.txt
  1. Add content to requirements.txt.
Flask
  1. Exit nano (Ctrl+x), save requirements.txt (y) and then press Enter.
  1. 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
  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)
  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
  1. Run the flask application:
python3 app.py

  1. View the results using the Web preview feature. Click the Web preview > Preview on port 8080:
CloudShell_web_preview.png
  1. A new tab will open to display your results:
This image has an empty alt attribute; its file name is Screenshot-2021-08-25-4.50.40-PM-1024x278.png
  1. Before continuing, stop the running node server by typing Ctrl-c in Cloud Shell.

Building the Docker image

  1. Use the following command to build the Docker image:
docker build -t quickstart-image .

2. 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:latest

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]
  • [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]
  1. 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

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]
  1. Run the following command, this will allow Docker to access your project’s Container Registry :
gcloud auth configure-docker

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

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

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

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:

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.

  1. The command format to pull your image from Container Registry is:
gcloud docker -- pull [HOSTNAME]/[PROJECT-ID]/[IMAGE]

2. Verify the authorization:

gcloud auth configure-docker
  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

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]
This image has an empty alt attribute; its file name is image-226.png
  1. See your pulled images:
gcloud container images list

Example output:

NAME
  [HOSTNAME]/[PROJECT-ID]/[IMAGE]
  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
  1. View the Hello World! results using the Web preview feature. Click Web preview > Preview on port 8080.

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

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
binance register
6 days ago

Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/en/register?ref=P9L9FQKY

Back To Top

Contact Us