How to build and push a docker image in ACR using the ADO pipeline

female software engineer coding on computer
Reading Time: 3 minutes

What is ACR?

ACR is known as Azure Container Registry. It is a cloud-based service for managing private registries on Docker Registry. We can use it to create and maintain Docker container registries in the Azure cloud and manage to store Docker images and artifacts privately.

These are the following steps to build and push the docker image to ACR using ADO:

Step1. Firstly you have to create the ACR registry through the Azure Portal. In the portal go to the container registry section and create the registry.

Step 2. After created the registry you have to go to the ADO and create a new project after created a new project go to the project setting.

Inside the project setting open the create service connections and there you have to select the Docker Registry.

After that you have to link the azure container registry that you want to connect.In the below image you can see the i am linking my ACR with ADO .Here registry0016 is my registry name.

Step 3. Select the repository where your source code and Dockerfile exist in my case it is in the Azure repository. If you have any other SCM you can add it here also.

Step 4. In the YAML file, I am creating tasks for build and pushing the image to ACR for that you have to select the Docker. In the task section attached to the container registry and command that is needed for build and push . You can select the build and push task together but here I am creating a separate task for build and push.

So my YAML file looks like this.

trigger:
- master

pool: agent-local
  
steps:
- script: 
    mvn clean install
    
- task: Docker@2
  inputs:
    containerRegistry: 'ACR Connection'
    repository: 'registry0016'
    command: 'build'
    Dockerfile: '**/Dockerfile'
  displayName: 'build docker image'


- task: Docker@2
  inputs:
    containerRegistry: 'ACR Connection'
    repository: 'registry0016'
    command: 'push'
  displayName: 'push docker image'

Step5. Now you have to run the pipeline, So In the jobs you can see there are two seprate task for build and push the image.

In the below image you can see the pipeline run successfully and image is pushed to Azure Container Registry.

Reference

Docs

Written by 

Harshit Gupta is a Software Consultant at knoldus Inc having few year experience in DevOps . He always eager to learn new technology and helping to others .

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading