0
0
GCPcloud~5 mins

Pushing and pulling images in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build software that runs in containers, you need a place to store those container images so you can use them later or share them. Pushing means sending your container image to a storage place called a registry. Pulling means getting that image back from the registry to run it on a computer.
When you want to save your container image to Google Container Registry so others can use it.
When you need to run your container on a Google Cloud server and must download the image first.
When you update your app and want to upload the new version of the container image.
When you want to share your container image with your team using Google Cloud.
When you want to move your container image between different computers or environments.
Commands
This command sets up your computer to securely talk to Google Container Registry by configuring Docker with your Google Cloud login.
Terminal
gcloud auth configure-docker
Expected OutputExpected
Docker configuration file updated. Credentials helper configured for gcr.io, us.gcr.io, eu.gcr.io, asia.gcr.io
This builds a container image from your current folder and tags it with the Google Container Registry path and version v1.
Terminal
docker build -t gcr.io/my-project/my-app:v1 .
Expected OutputExpected
Sending build context to Docker daemon 8.192kB Step 1/5 : FROM python:3.9-slim ---> 123abc456def Step 2/5 : COPY . /app ---> Using cache Step 3/5 : WORKDIR /app ---> Using cache Step 4/5 : RUN pip install -r requirements.txt ---> Using cache Step 5/5 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged gcr.io/my-project/my-app:v1
-t - Tags the image with a name and version
This uploads your tagged container image to Google Container Registry so it can be stored and shared.
Terminal
docker push gcr.io/my-project/my-app:v1
Expected OutputExpected
The push refers to repository [gcr.io/my-project/my-app] abcdef123456: Pushed v1: digest: sha256:abcdef1234567890 size: 1234
This downloads the container image tagged v1 from Google Container Registry to your local computer so you can run it.
Terminal
docker pull gcr.io/my-project/my-app:v1
Expected OutputExpected
v1: Pulling from my-project/my-app Digest: sha256:abcdef1234567890 Status: Downloaded newer image for gcr.io/my-project/my-app:v1 gcr.io/my-project/my-app:v1
Key Concept

If you remember nothing else from this pattern, remember: pushing sends your container image to the cloud registry, and pulling gets it back to run anywhere.

Common Mistakes
Trying to push an image without tagging it with the full registry path.
Docker does not know where to send the image without the full registry address, so push fails.
Always tag your image with the full Google Container Registry path like gcr.io/project-id/image-name:tag before pushing.
Not running 'gcloud auth configure-docker' before pushing or pulling.
Docker cannot authenticate with Google Container Registry, so push or pull commands fail with permission errors.
Run 'gcloud auth configure-docker' once to set up authentication for Docker.
Summary
Run 'gcloud auth configure-docker' to let Docker talk to Google Container Registry.
Build your container image and tag it with the full registry path.
Push the tagged image to Google Container Registry to store it.
Pull the image from the registry to download and run it anywhere.