Container Registry is a private container image registry that runs on Google Cloud.
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
-
Accede a tu Cuenta de Google.
Si todavía no tienes una cuenta, regístrate para obtener una nueva.
-
En GCP Console, ve a la página Administrar recursos y selecciona o crea un proyecto.
-
Comprueba que la facturación esté habilitada en tu proyecto.
- Habilita las Container Registry API necesarias.
- Realiza la instalación y la inicialización del SDK de Cloud.
- 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 usingsudo
:sudo usermod -a -G docker ${USER}
Log out and log back in for group membership changes to take effect. If you are using a virtual machine, you may need to restart the virtual machine for membership changes to take effect.
- Open Docker. To ensure that Docker is running, run the following Docker command, which returns the current time and date:
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:
Create a directory to store the three Docker image files.
In this directory, create three files:
Dockerfile
,requirements.txt
, andapp.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:3.5-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. For this quickstart, the host location is gcr.io
.
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 Console project ID, which you need to add to your command. If your project ID contains a colon (:
), see Domain-scoped projects.gcr.io
is the hostnamequickstart-image
is the name of the Docker imagetag1
is a tag you're adding to the Docker image. If you didn't specify a tag, Docker will apply the default taglatest
.
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 Console
project ID.
If your project ID contains a colon (:
), see
Domain-scoped projects.
When you push an image to a new host location, the service creates the
underlying storage bucket unique to your project. You can view images
hosted by Container Registry via the
Cloud 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 Console
project ID.
If your project ID contains a colon (:
), see
Domain-scoped projects.
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 account for the resources used in this quickstart, follow these steps.
Run the following command to delete the Docker image from Container Registry.
gcloud container images delete gcr.io/[PROJECT-ID]/quickstart-image:tag1 --force-delete-tags
where [PROJECT-ID]
is your Google Cloud Console
project ID.
If your project ID contains a colon (:
), see
Domain-scoped projects.