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 (docker.io) 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 acrname=myacr07 rgname=aksdemo location=eastus aksclustername=aksdemo # 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 docker.io/library/nginx:latest --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> <html> <body style="background-color:rgb(220, 240, 234);"> <h1>Welcome to https://rajanieshkaushikk.com</h1> <h2>Azure DevOps Demo for ACR and AKS</h2> <h2>Application Demo V1</h2> </body> </html>
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 myacr07.azurecr.io #Tag the image first docker tag nginxwithcustomwebpage myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage # Now push this image docker push myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage
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 myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage #Now try to pull the image and run it locally docker pull myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage docker run -it --rm -d -p 8080:80 --name nginxwithcustomwebpage7 myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage
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 metadata: name: nginx0-deployment labels: app: nginx0-deployment spec: replicas: 2 selector: matchLabels: app: nginx0 template: metadata: labels: app: nginx0 spec: containers: - name: nginx image: myacr07.azurecr.io/mycustomimage/nginxwithcustomwebpage ports: - 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.