Build And Pull Docker Images To ACR - Azure Container Registry


Nowadays, all developers are moving towards cutting edge technologies like Cloud computing, Containerization, Azure Container Registry, orchestration of an application/microservices, etc. Today we will discuss the below two major processes and data points in all these technologies. 
  • Build a .NET Core project’s Docker image build and pulling to Azure Container Registry- ACR
  • Steps for deploying an application to K8S (Kubernetes) using the KubeController command - kubectl in Azure CLI
  • Docker for desktop installed on a machine
  • Azure CLI installed on a machine 
  • Azure Resource Group in Azure account  

Build a Docker image

  1. Create any .NET core project in Visual Studio 2017/2019 with a containerization option which will create a docker file in it.
  2. After creating it, build a project.
  3. Open command a prompt and check whether KubeCLI installed proper or not by using below steps 
    • Window + Run 
    • Write cmd in Run
    • Write a kubectl and press Enter - which will give a detail that whether KuberController running or not
    • Now do Azure login on CLI using below command
      1. az login   
    • Now time to check to get access on Azure account, before that we have to list out all Azure account by below command
      1. az account list --output table  
      Which will give a list of subscriptions in table format. In this table, the default access value - True/False, will represent that on which subscription users have access.
    • This subscription can be changed by a user with the below command.
      1. Syntax - az account set --subscription "<NameOfSubscription>"  
      2. E.g. - az account set --subscription DemoSubscription  
    • Now set the project folder on the command prompt where a project is located
      1. Syntax - cd <Project Folder path>  
      2. E.g. - cd D:\DemoProject  
    • So, now time to build an image of project Docker file and pull it to the ACR using below command
      1. Syntax - az acr build --registry <ACrName From Azure> --image <imageName> --file <DockerFileLocationInProjectFolder> .  
      2. E.g. - az acr build --registry DemoACR --image test:v1 --file .\Project\Dockerfile .  
      Here in the above command developers have to take care at the time of the given path of the file. If the developer already on prompt of project folder in which "Docker" file located then used below command. Whereas Docker file is located in D:\Project folder
      1. az acr build --registry DemoACR --image test:v1 --file .\Dockerfile .  
      If user command prompt folder is 1-2 folder out then he/she must be reached to that folder in which Docker file is located and give above command
    • It's time to log in with AKS - Azure Kubernetes Service using below command
      1. Syntax - az aks get -credentials --name <NameOfAKS> --resource-group <NameOfResourceGroup> --admin  
      2. E.g. - az aks get-credentials --name MyManagedCluster --resource-group MyResourceGroup --admin   
    • Here, in the above command it's mandatory to use --admin because it will give the user access on AKS as admin so It will help the user further to do certain operations like service deployment and publishing on Kubernetes nodes and pods.
    • Now time to get a list of Nodes from Kube controller using the below command
      1. kubectl get nodes   ---this will list out all nodes name from all namespace  
      2. kubectl get nodes -n <NameSpaceName> ---- this will list out all node from specific namespace  
      After everything is set to deploy service to the AKS, before that, we have to create a YAML file for service deployment. Now, the question is arise what type of detail available in the YAML file. In the YAML file, we will set short of a configuration of service deployment and publishing detail. For understanding purposes, I put the YAML file example below.
Test YAML file - please consider it for understanding purposes:
  1. apiVersion: v1    
  2. kind: Service    
  3. metadata:    
  4.   name: demo1    
  5.   namespace: demo1    
  6. spec:    
  7.   type: NodePort     
  8.   ports:    
  9.   - port: 80    
  10.     targetPort: 80    
  11.     protocol: TCP    
  12.     name: http    
    • Before deployment of yaml file, we have to set that file path .
    • Deployment of an application to the AKS, use this yaml file which contains configuration related to AKS.
      1. Syntax - kubectl apply -f <yaml file name>  
      2. E.g. kubectl apply -f demo.yaml  
      After the creation of services, now its time to test services which will be Kubernetes service that exposes the application front end to the internet.
      1. Syntax - kubectl get service <serviceNameFromYamlfile> --watch  
      3. E.g. - kubectl get service demo1 --watch  
                  This command will give IP of service, so the user can consume it in the browser for further use.