Quickstart for Container Registry

Container Registry is a private container image registry that runs on Google Cloud Platform.

This quickstart shows you how to:

  • Build a Docker image
  • Push the image to your project's Container Registry
  • Pull the image from your project's Container Registry

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Container Registry API.

    Enable the API

  5. Install and initialize the Cloud SDK.
  6. Install Docker. If you're using a Linux-based operating system, such as Ubuntu or Debian, add your username to the docker group so that you can run Docker without using sudo:
    sudo usermod -a -G docker ${USER}

    You may need to restart your system after adding yourself to the docker group.

  7. Open Docker. To ensure that Docker is running, run the following Docker command, which returns the current time and date:
  8. docker run busybox date

For this quickstart, use either Cloud Shell or any environment where the Cloud SDK is installed.

Build a Docker image

For this quickstart, build the following Docker image so that you have an image to push to Container Registry. This Docker image contains a small Python web app. This app uses the Flask web framework to serve a web page which displays the message "Hello, World!"

To create the Docker image:

  1. Create a directory to store the three Docker image files.

  2. In this directory, create three files: Dockerfile, requirements.txt, and app.py. See the examples below for what you'll need to put in the contents of each file:

Dockerfile

# The Dockerfile defines the image's environment
# Import Python runtime and set up working directory
FROM python:2.7-slim
WORKDIR /app
ADD . /app

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

# Open port 80 for serving the webpage
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt

# This file defines the image's dependencies
Flask

app.py

# The Docker image contains the following code
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=80)

To build the Docker image, run the following Docker command from the directory containing the image's files:

docker build -t quickstart-image .

You've now created a Docker image on your local machine.

Add the image to Container Registry

Configure docker to use the gcloud command-line tool as a credential helper

Before you can push or pull images, you must configure Docker to use the gcloud command-line tool to authenticate requests to Container Registry. To do so, run the following command (you are only required to do this once):

gcloud auth configure-docker

Tag the image with a registry name

Before you push the Docker image to Container Registry, you need to tag it with its registry name. Tagging the Docker image with a registry name configures the docker push command to push the image to a specific location, which in this case is your project's Container Registry.

To tag the Docker image, run the following command:

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

where:

  • [PROJECT-ID] is your Google Cloud Platform Console project ID, which you need to add to your command
  • gcr.io is the hostname
  • quickstart-image is the name of the Docker image
  • tag1 is a tag you're adding to the Docker image. If you didn't specify a tag, Docker will apply the default tag latest.

You are now ready to push the image to Container Registry.

Push the image to Container Registry

Once docker has been configured to use gcloud as a credential helper, and the local image is tagged with the registry name, you can push it to Container Registry.

To push the Docker image, run the following command:

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

where:

  • [PROJECT-ID] is your Google Cloud Platform Console project ID

You can view images hosted by Container Registry via the GCP Console, or by visiting the image's registry name in your web browser: http://gcr.io/[PROJECT-ID]/quickstart-image.

Pull the image from Container Registry

To pull the image from Container Registry onto your local machine, run the following command:

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

where:

  • [PROJECT-ID] is your Google Cloud Platform Console project ID

You should see output similar to the following:

latest: Pulling from [PROJECT-ID]/quickstart-image:tag1
Digest: sha256:70c42...
Status: Image is up to date for gcr.io/[PROJECT-ID]/quickstart-image:tag1

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

Run the following command to delete the Docker image from Container Registry.

gcloud container images delete gcr.io/[PROJECT-ID]/quickstart-image:tag1

where:

  • [PROJECT-ID] is your Google Cloud Platform Console project ID

What's next

Send feedback about...

Container Registry