Build and push Docker Images to Azure Container Registry using Azure DevOps Pipelines

In this blog post I am going to show how you can build and push Docker Images to Azure Container Registry (ACR) using an Azure DevOps Pipeline.

Create Azure Container Registry

Run the below AZ CLI to create a resource group and azure container registry

Update the variables as highlighted

#!/bin/sh

ACR_RESOURCE_GROUP_NAME="tamopsimagesacr-rg"
ACR_NAME="tamopsimagesacr"

# Create a resource group to store container registry
az group create --name $ACR_RESOURCE_GROUP_NAME --location uksouth

# Create a container registry that will be where you deploy your image to
az acr create --resource-group $ACR_RESOURCE_GROUP_NAME --name $ACR_NAME --sku Basic

It will create as below

Docker Registry Service Connection creation

With the ACR deployed, we will now create a Docker Registry service connection within Azure DevOps

Inside Azure DevOps -> Project settings -> Service Connections -> Docker Service Connection

Select relevant subscription & newly created Azure container registry

I will create the service connection with name: tamopsimagesacr – this will be referenced within the pipeline that will be created

Azure DevOps Pipeline

Now that we have created the ACR & Docker Registry Service connection – lets breakdown the pipeline.

A simple aspnet-core application will be built and pushed to this ACR. Code for the application can be found [HERE]

Reviewing the variables

  • repository – The repository name that will be created on the ACR, this is where the newly created image will be
  • dockerfile – Location of Dockerfile within your Sources directory
  • containerRegistry – The Docker Service connection name that was created above
variables:
  - name: repository
    value: 'devopsjourney'
  - name: dockerfile
    value: '$(Build.SourcesDirectory)/aspnet-core-dotnet-core/Dockerfile'
  - name: containerRegistry
    value: 'tamopsimagesacr'

The pipeline Build stage, this stage will build and push the docker image to your ACR

Notice the tags reference? In this example, I will be tagging the image with the predefined $(Build.BuildId)

stages :
  - stage: Build
    displayName: Build and push stage
    jobs:  
    - job: Build
      displayName: Build job
      steps:
      - task: Docker@2
        displayName: Build and push an image to container registry
        inputs:
          command: buildAndPush
          repository: ${{ variables.repository }}
          dockerfile: ${{ variables.dockerfile }}
          containerRegistry: ${{ variables.containerRegistry }}
          tags: $(Build.BuildId)

Full pipeline

name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)

trigger: none

# Don't run against PRs
pr: none

variables:
  - name: repository
    value: 'devopsjourney'
  - name: dockerfile
    value: '$(Build.SourcesDirectory)/aspnet-core-dotnet-core/Dockerfile'
  - name: containerRegistry
    value: 'tamopsimagesacr'

stages :
  - stage: Build
    displayName: Build and push stage
    jobs:  
    - job: Build
      displayName: Build job
      steps:
      - task: Docker@2
        displayName: Build and push an image to container registry
        inputs:
          command: buildAndPush
          repository: ${{ variables.repository }}
          dockerfile: ${{ variables.dockerfile }}
          containerRegistry: ${{ variables.containerRegistry }}
          tags: $(Build.BuildId)

Reviewing Azure DevOps pipeline logs, you can see a successful Build and image an image to container registry

Reviewing the ACR repository devopsjourney you will see a new image available

Awesome! This blog post has shown how you can build and push Docker Images to Azure Container Registry (ACR) using an Azure DevOps Pipeline.

GitHub repository contains all configuration referenced above

3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s