Building and pushing container images to Azure Container Registry

When you want to develop and implement the container application in Azure. The first and foremost step you would execute is to build the images and push them into the Azure Container registry. In this article, I will explain how to achieve this objective.

There can be two options when you want to push the container images into ACR.

  • Option 1: Import the pre-existing Docker image from the docker hub ( registry and deploy it to AKS.
  • Option 2: Create a new custom image, push it to ACR, and then deploy it to AKS.

Building the environment

First, we will build ACR and AKS instances so we can run the rest of the Azure CLI commands.

#login to Azure
az login

# Set variables

# Create Resource Group
az group create -l $location -n $rgname

# Create ACR instance
az acr create -n $acrname -g $rgname --sku basic

#Create AKS instance
az aks create -n $aksclustername -g $rgname --generate-ssh-keys --attach-acr $acrname

Pushing pre-existing image to ACR

In this example, we will take the pre-existing docker NGINX image from the Docker hub and import it into ACR.

az acr import  -n $acrname --source --image nginx:v1

Build a new custom image and push it to ACR

In this example, we will build a new custom image with a Docker file and push it into ACR, and test it. In this example, we have built a custom NGINX image.

docker build -t nginxwithcustomwebpage . -f Dockerfile.txt
docker run -it --rm -d -p 8080:80 --name nginxmywebpage nginxwithcustomwebpage

Here is the explanation of the docker file. It takes the base image of NGINX and copies the index.html kept in the local directory into NGINX. This HTML file contains the custom HTML markup.

FROM nginx
COPY /index.html /usr/share/nginx/html/index.html

Here is the HTML page. It is a very simple HTML page (index.html) that will be copied into NGINX:

<!DOCTYPE html>
   <body style="background-color:rgb(220, 240, 234);">
      <h1>Welcome to</h1>
      <h2>Azure DevOps Demo for ACR and AKS</h2>
      <h2>Application Demo V1</h2>

Now let’s push this image to ACR. For this first we need to login to ACR and push the image to ACR:

az acr login --name

#Tag the image first
docker tag nginxwithcustomwebpage

# Now push this image
docker push

Test if we can pull from ACR and run locally

In this step, we will test if we can pull the custom image from ACR and run it locally. So first we will delete the existing image and then pull the image from ACR.

#Testing if Image works

#First remove the local image and delete the runing container from UI of Docker desktop
docker image rm

#Now try to pull the image and run it locally
docker pull

docker run -it --rm -d -p 8080:80 --name nginxwithcustomwebpage7 

Now we can see that we are able to run the same image on localhost. See carefully we have exposed container local port to machine port 8080.

What if I want to deploy this image to AKS?

Now for the purpose of deploying this file to AKS, we need to prepare the deployment YAML file.

apiVersion: apps/v1
kind: Deployment
  name: nginx0-deployment
    app: nginx0-deployment
  replicas: 2
      app: nginx0
        app: nginx0
      - name: nginx
        - containerPort: 80

Now we will use kubectl command to create the deployment.

kubectl apply -f deployment.yaml

Now let’s test if it is deployed to AKS or not.

Hope you found it to be useful.

+ There are no comments

Add yours

Leave a Reply