title | description | ms.topic | ms.assetid | ms.author | ms.date | monikerRange | zone_pivot_groups |
---|---|---|---|---|---|---|---|
Use Azure Pipelines to build and push container images to registries |
Use Azure Pipelines to build and push container images to Docker Hub and Azure Container Registry. |
tutorial |
3ce59600-a7f8-4a5a-854c-0ced7fdaaa82 |
jukullam |
12/20/2024 |
azure-devops |
pipelines-docker-acr |
[!INCLUDE version-eq-2022]
This article guides you through the creation of a pipeline to build and push a Docker image to an Azure Container Registry or Docker Hub.
:::zone pivot="acr-registry"
Product | Requirements |
---|---|
Azure DevOps | - An Azure DevOps project. - Permissions: - To grant access to all pipelines in the project: You must be a member of the Project Administrators group. - To create service connections: You must have the Administrator or Creator role for service connections. - If you're using a self-hosted agent, ensure Docker is installed and the Docker engine is running with elevated privileges. Microsoft-hosted agents have Docker preinstalled. |
GitHub | - A GitHub account. - A GitHub repository with a Dockerfile. Use the sample repository if you don't have your own project. - A GitHub service connection to authorize Azure Pipelines. |
Azure | - An Azure subscription. - An Azure Container Registry. |
:::zone-end
::: zone pivot="docker-registry"
Product | Requirements |
---|---|
Azure DevOps | - An Azure DevOps project. - Permissions: - To grant access to all pipelines in the project: You must be a member of the Project Administrators group. - To create service connections: You must have the Administrator or Creator role for service connections. - If you're using a self-hosted agent, ensure Docker is installed and the Docker engine is running with elevated privileges. Microsoft-hosted agents have Docker preinstalled. |
GitHub | - A GitHub account. - A GitHub repository with a Dockerfile. Use the sample repository if you don't have your own project. - A GitHub service connection to authorize Azure Pipelines. |
Docker Hub | - A Docker Hub account. - A Docker Hub image repository. |
Before pushing container images to a registry, you need to create a service connection in Azure DevOps. This service connection stores the credentials required to securely authenticate with the container registry. For more information, see Docker Registry service connections.
-
In your Azure DevOps project, select Project settings > Service connections.
:::image type="content" source="../media/project-settings-selection.png" alt-text="Screenshot of Project settings selection.":::
-
Select New service connection and Docker Registry.
:::image type="content" source="../media/docker-registry-selection.png" alt-text="Screenshot of Docker Registry selection.":::
-
Select Docker Hub and enter the following information:
Field Description Docker ID Enter your Docker ID. Docker Password Enter your Docker password. Service connection name Enter a name for the service connection. Grant access permission to all pipelines Select this option to grant access to all pipelines. :::image type="content" source="../media/docker-hub-service-connection-dialog.png" alt-text="Screenshot of Docker Hub service connection dialog.":::
-
Select Verify and save.
:::zone-end
::: zone pivot="docker-registry"
The Docker@2 task is used to build and push the image to the container registry. The Docker@2 task is designed to streamline the process of building, pushing, and managing Docker images within your Azure Pipelines. This task supports a wide range of Docker commands, including build, push, login, logout, start, stop, and run.
Use the following steps to create a YAML pipeline that uses the Docker@2 task to build and push the image.
-
In to your Azure DevOps project, select Pipelines and New pipeline.
-
Select GitHub as the location of your source code and select your repository.
- If you're redirected to GitHub to sign in, enter your GitHub credentials.
- If you're redirected to GitHub to install the Azure Pipelines app, select Approve and install.
-
Select your repository.
-
Select the Starter pipeline template to create a basic pipeline configuration.
-
Replace the contents of azure-pipelines.yml with the following code:
trigger: - main pool: vmImage: 'ubuntu-latest' variables: repositoryName: '<target repository name>' steps: - task: Docker@2 inputs: containerRegistry: '<docker registry service connection>' repository: $(repositoryName) command: 'buildAndPush' Dockerfile: '**/Dockerfile'
-
Edit the pipeline YAML file as follows:
- Replace
<target repository name>
with the name of the repository in the container registry where you want to push the image. - Replace
<docker registry service connection>
with the name of the Docker registry service connection you created earlier.
- Replace
-
When you're done, select Save and run > Save and run.
-
Select Job to view the logs and verify the pipeline ran successfully.
-
From your Azure DevOps project, select Pipelines and New pipeline
-
Select Use the classic editor from the Where is your code? page.
-
On the Select a source page, select GitHub.
-
Choose your repository and select Continue.
-
On the Select a template page, select Empty pipeline and Apply.
-
Select ubuntu latest for the Agent Specification.
:::image type="content" source="../media/classic-docker-pipeline-dialog.png" alt-text="Screenshot of classic Docker pipeline.":::
To build and push the image to the container registry, add the Docker@2 task to the pipeline.
-
Add a task to the Agent job 1.
:::image type="content" source="../media/classic-pipeline-add-task.png" alt-text="Screenshot of add task icon.":::
-
Select the Docker task, and Add.
-
Select the buildAndPush task.
-
For Container Registry, select the service connection you created earlier. If you don't have one, select +New to create a new Docker Hub service connection.
:::image type="content" source="../media/classic-pipeline-docker-2-build-and-push-task.png" alt-text="Screenshot of task to build and push image to Docker Hub.":::
- Select Save and queue > Save and Queue.
- On the Run pipeline page, select Save and run.
- Select Job to view the logs and verify the pipeline ran successfully.
:::zone-end
::: zone pivot="acr-registry"
-
Go to your Azure DevOps project and select Pipelines from the left-hand menu.
-
Select New pipeline.
-
Select GitHub as the location of your source code and select your repository.
- If you're redirected to GitHub to sign in, enter your GitHub credentials.
- If you're redirected to GitHub to install the Azure Pipelines app, select Approve and install.
-
Select the Docker - Build and push an image to Azure Container Registry template.
-
Select your Azure subscription and Continue.
-
Select your Container Registry, and then select Validate and configure.
Example YAML pipeline:
# Docker # Build and push an image to Azure Container Registry # https://docs.microsoft.com/azure/devops/pipelines/languages/docker trigger: - main resources: - repo: self variables: # Container registry service connection established during pipeline creation dockerRegistryServiceConnection: '7f9dc28e-5551-43ee-891f-33bf61a995de' imageRepository: 'usernamepipelinesjavascriptdocker' containerRegistry: 'repoistoryname.azurecr.io' dockerfilePath: '$(Build.SourcesDirectory)/app/Dockerfile' tag: '$(Build.BuildId)' # Agent VM image name vmImageName: 'ubuntu-latest' stages: - stage: Build displayName: Build and push stage jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - task: Docker@2 displayName: Build and push an image to container registry inputs: command: buildAndPush repository: $(imageRepository) dockerfile: $(dockerfilePath) containerRegistry: $(dockerRegistryServiceConnection) tags: | $(tag)
-
Select Save and run and Save and run again.
-
Select Job to view the logs and verify the pipeline ran successfully.
The Docker template creates the service connection to your Azure Container Registry and uses the Docker@2 task to build and push the Docker image to the registry.
The Docker@2 task is designed to streamline the process of building, pushing, and managing Docker images within your Azure Pipelines. This task supports a wide range of Docker commands, including build, push, login, logout, start, stop, and run.
-
From your Azure DevOps project, select Pipelines and New pipeline.
-
Select Use the classic editor from the Where is your code? page.
-
On the Select a source page, select GitHub.
-
Choose your repository and select Continue.
-
On the Select a template page, select Empty pipeline and Apply.
-
Select ubuntu latest for the Agent Specification.
:::image type="content" source="../media/classic-docker-pipeline-dialog.png" alt-text="Screenshot of classic Docker pipeline editor.":::
Add a Docker@2 task to the pipeline to build and push the image to the container registry.
-
Add a task to Agent job 1.
:::image type="content" source="../media/classic-pipeline-add-task.png" alt-text="Screenshot of add task icon.":::
-
Select the Docker task, and Add.
-
Select the buildAndPush task.
-
Create a service connection select +New.
:::image type="content" source="../media/classic-pipeline-new-service-connection-selection.png" alt-text="Screenshot of new service connection selection.":::
-
Fill in the following fields:
Field Description Subscription Select your Azure subscription. Azure container registry Select your Azure container registry. Service connection name Enter a name for the service connection. Grant access permission to all pipelines Select this option to grant access to all pipelines. :::image type="content" source="../media/classic-pipeline-new-azure-container-registry-service-connection-dialog.png" alt-text="Screenshot of new Azure Container Registry service connection.":::
-
Select Save.
- Select Save and queue > Save and Queue.
- On the Run pipeline page, select Save and run.
- Select Job to view the logs and verify the pipeline ran successfully.
:::zone-end
When using self-hosted agents, be sure that Docker is installed on the agent's host, and the Docker engine/daemon is running with elevated privileges.