Quickstart for Google Container Registry

This quickstart demonstrates how to build a Docker image and push it to Google 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 Cloud Platform Console, go to the Projects page and select or create a new project.

    Go to the Projects page

  3. Enable billing for your project.

    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}

    For this command to take effect, 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

Building the Docker image

At minimum, a Docker image consists of three files: a Dockerfile, a requirements.txt file, and your code.Dockerfile defines your image's environment, and requirements.txt defines your image's dependencies.

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:

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

  2. In your Docker image 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

# 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

Flask

app.py

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 your Docker image directory:

docker build -t hello .

You have now created a Docker image!

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 format is below:

[HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

where

  • [HOSTNAME]is the gcr.io hostname
  • [YOUR-PROJECT-ID] is your Google Cloud Platform Console project ID
  • [IMAGE] is your image's name.

For this quickstart, use us.gcr.io as your hostname.

To tag your Docker image for Container Registry, run the following Docker command:

docker tag [IMAGE] [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

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

Pushing your image

To push your Docker image to Container Registry, run the following command:

gcloud docker -- push [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

You can view images hosted by Container Registry via the Cloud Platform Console, or by visiting the image's registry name in your web browswer: http://[HOSTNAME]/[PROJECT-ID]/[IMAGE].

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.

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

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

You should see output similar to the following:

  latest: Pulling from [YOUR-PROJECT-ID]/[IMAGE]
  Digest: sha256:70c42...
  Status: Image is up to date for [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

Run the gcloud container images list command to see your pulled images:

  NAME
  [HOSTNAME]/[YOUR-PROJECT-ID]]/[IMAGE]

To run an instance of your image, run the following Docker command:

docker run -p 80:80 [HOSTNAME]/[YOUR-PROJECT-ID]]/[IMAGE]

If the output is similar to * Running on http://0.0.0.0:80/ (Press CTRL+C to quit), your instance is running! To view the results, open your web browser to http://localhost:80.

Clean up

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

Delete your Docker image and Cloud Storage bucket

  1. To delete your Docker image, run the following command:

    gcloud container images delete [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]
    
  2. Run the gsutil ls command to see your project's Cloud Storage buckets.

  3. To delete your Cloud Storage bucket, run the following command:

    gsutil rb gs://[YOUR-STORAGE-BUCKET]
    

    where gs://[YOUR-STORAGE-BUCKET] is the full bucket name displayed by gsutil ls.

Delete your project

  1. Go to the Cloud Platform Console.
  2. Click the check box beside your project, then click Delete Project. This schedules the project for deletion.

What's next

Send feedback about...

Container Registry