GCR - Use Docker CLI with Google Container Registry

In this short tutorial you’ll walk through a few simple steps to allow the Docker CLI to work with Google Container Registry.

Overview

Google Container Registry provides secure private container image storage that is scoped to a specific GCP project.

The Docker command line tool can push to container registries other than Dockerhub. To do this, the CLI needs proper credentials and the image needs to be named appropriately

The gcloud cli provides a helper gcloud auth configure-docker that allows docker to use the gcloud credentials.

The proper name of the image should contain the location of the image on the network. The docker push command will know where the repository is by the image name, such as: docker push [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

In this short tutorial you’ll walk through the steps to allow access through native docker commands.

  • Configure Docker to use gcloud credentials
  • Create a simple containerized Application
  • Build the application with docker
  • Push the image to GCR.io

Prepare your environment

Set some variables

export PROJECT_ID=$(gcloud config get-value project)
export APP_NAME=hello_python
export APP_VERSION=1.0

Enable the Container Registry API

cloud services enable \
    containerregistry.googleapis.com 

Configure Docker to use gcloud credentials

gcloud auth configure-docker

Create a simple containerized Application

mkdir hello-python && cd hello-python
cat <<EOF > app.py
import os

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World v1.0'

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=8080)
EOF
cat <<EOF > Dockerfile
FROM python:3.7-slim

WORKDIR /app
COPY . ./
RUN pip install Flask gunicorn
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app
EOF

Build the application with docker

docker build -t gcr.io/${PROJECT_ID}/${APP_NAME}:${APP_VERSION} .

Push the image to GCR.io

docker push gcr.io/${PROJECT_ID}/${APP_NAME}:${APP_VERSION}