Hello readers! This article talks about the way to Build and Push Docker Images to Azure Container Registry with a practical demonstration.
So, let us begin!!
Quick Recap – Dockerfile and Docker Images
With the increasing demand and supply chain in every sector, it is important for any business/technology product to be able to scale with the increasing demands.
Relating it to the cloud world, we have seen the transition right from on-premise servers to Virtual Machines. And, now we are here with a more packaged way of scalability with Containers. Containers enable us to encapsulate every aspect and library (configuration files) for an application to run efficiently. Thus, it has led to introducing more scalability in the application/product with cost optimization.
To benefit from containers, we need to start building the Dockerfile. It is the configuration file that consists of all the instructions to feed the application for it to run in the form of a container.
Once the Dockerfile is ready, we build a Docker image out of it. The Docker Image is a blueprint of the entire application execution process.
In order to roll out a container, we need to provision the Docker Image to store in some Registry. It can be a public registry like the Docker Hub or a private one like the Google Container Registry (GCR) or the Azure Container Registry (ACR).
Private container registries tend to be a more secure place for our Docker Image as the image is the blueprint of the entire product and it is not feasible to be compromised under any circumstance. In order to use an image from a private registry, we need to authenticate the service account/ Service Principle and use Image pull secrets in the Kubernetes Deployment YAML file.
In this article, we will be focusing on Pushing the image to Azure Container Registry.
Build and Push Docker Image to Container Registry – Steps
Let us now begin the process to build and push a Docker Image to ACR. Below are the steps to push an Image to a Container Registry-
- Create a Dockerfile
- Provision a Docker Image
- Authenticate with the Azure Container Registry
- Tag the Docker Image
- Push the Image to ACR
Step 1: Create a Dockerfile
The Dockerfile should include all the configuration files/libraries that are needed to run the application/product.
Have a look at the below Dockerfile-
FROM python:alpine3.12 RUN curl -LO https://dl.k8s.io/release/v1.19.7/bin/linux/amd64/kubectl | bash && chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz WORKDIR /usr/src/application COPY . . ADD start.py /usr/src/application ENTRYPOINT ["python3","start.py"]
This is a demo Dockerfile that is built over the base python-alpine image. It also contains commands/instructions to install some packages such as gcloud, kubectl, etc during the application execution.
Step 2: Provision a Docker Image
Having created a Dockerfile, it is time to build an Image to over it using the below command-
docker build -t image_name:tag .
Once the image is built, you can find it locally on your system using the below command-
Step 3: Authenticate with the Azure Container Registry
In order to push Images to ACR, we need to set up a Container Registry within the Azure Subscription.
Once the container registry is ready, we can view it under the resource group within the subscription of Azure as seen below-
We now need to authenticate ourselves to the subscription which holds the container registry. This can be done using the below commands-
az login az account set --subscription subscription_ID
At last, we login to the Container registry using the below command-
az acr login --name docsamACR
Step 4: Tag the Image and push to ACR
We now need to tag the image that we built in Step 2 using the below command-
docker tag hello-world:latest docsamACR.azurecr.io/demoimage:latest
And as the final step, we need to push the tagged image to ACR.
docker push docsamACR.azurecr.io/demoimage:latest
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!! 🙂