Hello, readers. This article talks about the process to Push a Docker Image to Google Container Registry with a practical demonstration.
So, let us begin! 😊
What is a Docker Image?
Docker Image is the blueprint of an application code with instructions into it to build a container. We can containerize an application through a Dockerfile which has all the instructions to execute an application code.
This Dockerfile ‘s product is the Docker Image which has the instructions embedded.
What is a Google Container Registry?
A Container Registry is used to store docker images in a secured manner. With Container Registry, the docker images get stored considering it as a private registry.
Container registries can be public or private. When we plan to use a private registry, it simply means that our images will be stored privately and won’t be available to the external world publicly.
We can push and pull images to and from the Container Registry easily with Google’s Container Registry.
Practical Implementation – Push Docker Image to GCR
Having understood about Docker Images and Google Container registry, now let us understand the step-by-step process to Push Docker Image to GCR.
Step 1: Create a Docker Image locally out of a Dockerfile using Docker Desktop CLI
At first, we need to have a Docker Image ready to be pushed or published to the Google Container Registry.
For the same, we create a Dockerfile that contains instructions to the executed for a specific demo (python) application. Once ready, we build the docker image out of it as shown below—
Having built a Dockerfile, now let us create an image out of it using the below command on our local workstations-
docker build -t demo_python:1.0 .
We tag the docker image with the -t flag within the command. Once the image is built, we confirm its presence in our local environment using the below command-
Step 2: Get the JSON credentials of the Service account for the GCR push process
In order to push the Docker Image to Google Container Registry, we need some kind of authentication mechanism that enables us to authenticate us with the GCR for storing the images.
For the same, we need to have a Google Service account in place that has the below permissions within the Google Cloud Project where the GCR is located.
- Compute Storage Admin
- Storage Object Viewer
With the above permissions, we authenticate to push images to the GCR through the defined service account.
In our case the service account having the above permissions is –
Download the JSON key of the above service account through the Manage Keys option.
Step 3: Login to Google cloud through the service account
We now make use of the service account to log in to the console. For the same, we log in using the downloaded JSON key for proper authentication.
gcloud auth activate-service-account --key-file gcpsvckey.json
Activated service account credentials for: [email@example.com]
Once we are authenticated, let us now set the project within which we have created our GCR to push/pull Images.
gcloud config set project project_ID
Step 4: Configure docker
Let us now configure docker to add the registry’s host details into the config.json file at the backend.
gcloud auth configure-docker
Step 5: Tag the docker image on your local environment
We will now be tagging the original image to a specific tag on our local workstation.
docker tag demo_python:1.0 eu.gcr.io/project-01/demo:latest
Step 6: Finally, push your image to GCR using the below command
Having tagged the image, let us now push the image to the Google Container Registry using the below command-
docker push demo_python:1.0 eu.gcr.io/project-01/demo:latest
Executing the above command should reflect the image to the GCR.
By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Kubernetes and Docker, stay tuned with us.
Till then, Happy Learning!! 🙂