Skip to content
Latchkey

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@2 with command: buildAndPush.
  • Set containerRegistry, repository, dockerfile, and tags.

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 image

Gotchas

  • buildAndPush runs both build and push; use build or push alone for finer control.
  • Each newline in tags is a separate tag applied to the same image.
  • The service connection must have push permission on the target registry.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →