How to Push a Docker Image to a Container Registry from Azure DevOps Pipeline
In modern CI/CD workflows, Docker containers have become an essential part of deploying and managing applications. When working with Azure DevOps, it’s easy to build your Docker images in a pipeline and then push them to a container registry, such as Azure Container Registry (ACR) or Docker Hub. This blog post will guide you step-by-step through setting up an Azure DevOps pipeline to push a Docker image to a container registry.
Step 1: Set Up Your Azure DevOps Project 🛠️
Before anything else, you need an Azure DevOps project where you’ll configure your pipeline.
-
Create a Project: If you haven’t already, create a new project in Azure DevOps.
-
Set Up Repositories: Ensure that your code (along with a
Dockerfile) is stored in a Git repository within Azure DevOps or linked to an external Git repository.
Step 2: Create or Link Your Container Registry 🐳
You can push your Docker images to different container registries:
-
Azure Container Registry (ACR)
-
Docker Hub
-
Private Container Registry
For this guide, let’s focus on Azure Container Registry (ACR) and Docker Hub.
-
For ACR:
-
Create an Azure Container Registry in the Azure portal if you don’t already have one.
-
You’ll need the login server name (
<registry-name>.azurecr.io), username, and password to authenticate with ACR. -
For Docker Hub:
-
You’ll need your Docker Hub username and password for authentication.
Step 3: Configure the Azure DevOps Pipeline YAML File 📄
Now, let’s dive into how to configure your YAML pipeline to build and push Docker images.
1. Pipeline YAML for Azure Container Registry:
Here's a YAML example to build and push a Docker image to ACR:trigger: - main pool: vmImage: 'ubuntu-latest' variables: imageName: 'my-app' steps: - task: DockerInstaller@0 displayName: 'Install Docker' # Login to Azure Container Registry - task: AzureCLI@2 inputs: azureSubscription: '<Your Azure Subscription>' scriptType: bash scriptLocation: inlineScript inlineScript: | az acr login --name <Your ACR Name> # Build the Docker image - task: Docker@2 displayName: 'Build Docker Image' inputs: command: build repository: $(imageName) dockerfile: '**/Dockerfile' tags: | $(imageName):$(Build.BuildId) $(imageName):latest # Push the Docker image to ACR - task: Docker@2 displayName: 'Push Docker Image' inputs: command: push repository: $(imageName) tags: | $(imageName):$(Build.BuildId) $(imageName):latest
2. Pipeline YAML for Docker Hub:
For Docker Hub, you’ll need to log in with your Docker Hub credentials. Here’s how the YAML pipeline would look:trigger: - main pool: vmImage: 'ubuntu-latest' variables: dockerHubImageName: 'your-dockerhub-username/my-app' dockerHubUsername: '$(dockerHubUsername)' # Set as pipeline secrets dockerHubPassword: '$(dockerHubPassword)' # Set as pipeline secrets steps: - task: DockerInstaller@0 displayName: 'Install Docker' # Login to Docker Hub - task: Docker@2 displayName: 'Login to Docker Hub' inputs: command: login containerRegistry: 'Docker Hub' dockerRegistryEndpoint: '<Docker Hub Service Connection>' # Build the Docker image - task: Docker@2 displayName: 'Build Docker Image' inputs: command: build repository: $(dockerHubImageName) dockerfile: '**/Dockerfile' tags: | $(dockerHubImageName):$(Build.BuildId) $(dockerHubImageName):latest # Push the Docker image to Docker Hub - task: Docker@2 displayName: 'Push Docker Image' inputs: command: push repository: $(dockerHubImageName) tags: | $(dockerHubImageName):$(Build.BuildId) $(dockerHubImageName):latest
Step 4: Set Up Azure DevOps Service Connections 🔑
To securely authenticate Azure DevOps with your container registry, you need to configure service connections.
1. Azure Container Registry:
-
Navigate to your Azure DevOps project > Project Settings > Service Connections.
-
Create a new service connection for Azure Resource Manager to connect to your ACR instance.
2. Docker Hub:
-
Go to Service Connections and create a new Docker Registry connection.
-
Provide your Docker Hub credentials (username and access token/password) here.
Step 5: Run the Pipeline ✅
Once your pipeline is configured, push your YAML file to the repository. Azure DevOps will automatically trigger the pipeline if you set up a trigger on the main branch. You can also manually trigger the pipeline from the Azure DevOps interface.
-
The pipeline will:
-
Install Docker.
-
Log into your container registry (ACR or Docker Hub).
-
Build the Docker image.
-
Push the image to the specified container registry with a unique tag.
Best Practices for Managing Docker Images ⚙️
-
Use Proper Tagging: Always tag your Docker images properly. For instance, using the
latesttag for your most recent image and version-specific tags for historical builds. -
Clean Up Old Images: Over time, your container registry might accumulate many outdated images. Set up a cleanup policy to remove unused or old images to avoid hitting storage limits.
-
Leverage Azure DevOps Secrets: Never hard-code sensitive data like passwords in your YAML. Always use Azure DevOps secrets and service connections to manage sensitive information securely.
Conclusion 🌟
Pushing Docker images to a container registry from Azure DevOps is a streamlined and efficient process. Whether you're using Azure Container Registry (ACR) or Docker Hub, you can automate the entire process, ensuring consistent, reliable Docker builds and deployments.
By following the steps outlined above, you'll be able to set up a robust CI/CD pipeline, ensuring your Docker images are ready for use in development, testing, or production environments. Happy building! 🛠️
Imported from rifaterdemsahin.com · 2025