0
0
GcpHow-ToBeginner · 4 min read

How to Use Container Registry on GCP: Simple Guide

To use Google Cloud Container Registry, first tag your Docker image with the registry name, then push it using docker push. You can pull images later with docker pull and deploy containers from these images on GCP services.
📐

Syntax

Here is the basic syntax to tag and push a Docker image to Google Container Registry (GCR):

  • docker tag SOURCE_IMAGE gcr.io/PROJECT_ID/IMAGE_NAME:TAG - Tags your local image for GCR.
  • docker push gcr.io/PROJECT_ID/IMAGE_NAME:TAG - Uploads the tagged image to GCR.
  • docker pull gcr.io/PROJECT_ID/IMAGE_NAME:TAG - Downloads the image from GCR.

Replace PROJECT_ID with your Google Cloud project ID, IMAGE_NAME with your image name, and TAG with a version or label.

bash
docker tag my-app gcr.io/my-project/my-app:v1
docker push gcr.io/my-project/my-app:v1
docker pull gcr.io/my-project/my-app:v1
💻

Example

This example shows how to build a Docker image, tag it for GCR, push it, and then pull it back.

bash
gcloud auth configure-docker

docker build -t my-app .
docker tag my-app gcr.io/my-project/my-app:v1
docker push gcr.io/my-project/my-app:v1
docker pull gcr.io/my-project/my-app:v1
Output
Docker image 'my-app' built successfully. The image is tagged as 'gcr.io/my-project/my-app:v1'. Pushed image to gcr.io/my-project/my-app:v1. Pulled image from gcr.io/my-project/my-app:v1.
⚠️

Common Pitfalls

Common mistakes when using GCP Container Registry include:

  • Not authenticating Docker with GCP using gcloud auth configure-docker, causing push/pull failures.
  • Using incorrect project IDs or image names in tags.
  • Forgetting to enable the Container Registry API in your GCP project.
  • Not having proper IAM permissions to push or pull images.
bash
Wrong:
docker push gcr.io/wrong-project/my-app:v1

Right:
gcloud auth configure-docker
docker push gcr.io/my-project/my-app:v1
📊

Quick Reference

CommandPurpose
gcloud auth configure-dockerSet up Docker authentication with GCP
docker build -t IMAGE .Build a Docker image locally
docker tag IMAGE gcr.io/PROJECT_ID/IMAGE_NAME:TAGTag image for GCR
docker push gcr.io/PROJECT_ID/IMAGE_NAME:TAGPush image to GCR
docker pull gcr.io/PROJECT_ID/IMAGE_NAME:TAGPull image from GCR

Key Takeaways

Always authenticate Docker with GCP using 'gcloud auth configure-docker' before pushing or pulling images.
Tag your Docker images with the full GCR path including your project ID before pushing.
Enable Container Registry API and have proper permissions in your GCP project.
Use consistent image names and tags to manage versions clearly.
You can deploy containers from these images on GCP services like Cloud Run or GKE.