Introduction
As containerized applications become the norm in modern DevOps workflows, having an automated CI/CD pipeline to build, test, and deploy Docker images is essential. Azure Pipelines — a part of Azure DevOps — provides a powerful, cloud-native platform to manage the entire lifecycle of your Docker applications, from source control to deployment.
In this blog, you'll learn how to:
- Build Docker images in Azure Pipelines
- Push them to Azure Container Registry (ACR) or Docker Hub
- Deploy them to services like Azure Kubernetes Service (AKS)
Prerequisites
Before starting, ensure you have:
- A Git repository (Azure Repos or GitHub)
- Dockerfile in your app's root directory
- Azure DevOps organization and project
- Azure Container Registry (or Docker Hub) created
Step 1: Create an Azure Pipeline (YAML)
In your repo, create a azure-pipelines.yml
file to define your pipeline steps.
Sample Pipeline to Build and Push Docker Image to Azure Container Registry (ACR):
trigger:
branches:
include:
- main
variables:
imageName: myapp
registry: myacr.azurecr.io
stages:
- stage: Build
jobs:
- job: DockerBuild
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
- task: DockerInstaller@0
inputs:
dockerVersion: '20.10.7'
- task: Docker@2
inputs:
containerRegistry: 'MyACRServiceConnection'
repository: '$(imageName)'
command: 'buildAndPush'
Dockerfile: '**/Dockerfile'
tags: |
latest
$(Build.BuildId)
Note:
- Replace
MyACRServiceConnection
with a valid Azure DevOps service connection to your ACR. - Set
registry
variable to your ACR login server (myacr.azurecr.io
).
Step 2: Configure Azure Container Registry Service Connection
To allow Azure DevOps to push images to ACR:
- Go to Project Settings → Service connections
- Click + New service connection → Docker Registry
- Choose Azure Container Registry
- Authenticate using Azure Resource Manager and select your ACR
- Name it (e.g.,
MyACRServiceConnection
) and save
Step 3: (Optional) Deploy to Azure Kubernetes Service
You can extend the pipeline to deploy your Docker image to AKS.
- stage: Deploy
dependsOn: Build
jobs:
- job: DeployToAKS
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Kubernetes@1
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'MyAzureRMConnection'
azureResourceGroup: 'myResourceGroup'
kubernetesCluster: 'myAKSCluster'
command: apply
useConfigurationFile: true
configuration: 'manifests/deployment.yaml'
Make sure your Kubernetes deployment YAML (manifests/deployment.yaml
) pulls the latest Docker image from ACR using the same tag.
Conclusion
With Azure Pipelines, you can easily build and release Dockerized applications to any container registry or hosting environment. This CI/CD workflow ensures consistency, scalability, and speed in your software delivery process.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.