How to Build and Push a Container Image in Azure Pipelines
The Docker@2 task with the buildAndPush command builds your Dockerfile and pushes the tagged image to a registry in one step.
Create a Docker Registry service connection, then use Docker@2 with command: buildAndPush, the containerRegistry connection, a repository, and a tag such as the build id.
Steps
- Create a Docker Registry service connection for your registry.
- Add
Docker@2withcommand: buildAndPush. - Set
containerRegistry,repository,dockerfile, andtags.
Pipeline
azure-pipelines.yml
steps:
- task: Docker@2
inputs:
command: buildAndPush
containerRegistry: 'acr-connection'
repository: checkout-api
dockerfile: '**/Dockerfile'
tags: |
$(Build.BuildId)
latest
displayName: Build and push imageGotchas
buildAndPushruns both build and push; usebuildorpushalone for finer control.- Each newline in
tagsis a separate tag applied to the same image. - The service connection must have push permission on the target registry.
Related guides
How to Run a Job in a Container in Azure PipelinesRun every step of an Azure Pipelines job inside a Docker image with the container keyword, so the whole job u…
How to Deploy a Web App With the AzureWebApp Task in Azure PipelinesDeploy a built package to Azure App Service from Azure Pipelines with the AzureWebApp@1 task, pointing it at…