Blog Post

Microsoft Developer Community Blog
4 MIN READ

Azure DevOps - Docker Release Pipeline

chilberto's avatar
chilberto
Iron Contributor
Oct 29, 2019

This article provides an example of using Azure DevOps to manage the release pipeline of a Docker application. The goal of this article is to provide a practical example that can be used as a basis for more complex scenarios.

Setup

Let's start with a simple container and instead of spending time on how to construct this let's start as if we have completed the Microsoft Docs Tutorial: Containerize a .NET Core app. If you are not familiar with building a docker app, please refer to the tutorial to get started.

 

A copy of the source in preparation for this article has been prepared here. If you want to follow along, then the easiest thing to do is clone the project as a starting point. This can be done by accessing the project in GitHub: TechCommunity-DockerPipeline and performing a clone.

 

An Azure DevOps account will also be required. You can get started with the setup by following the steps in the Microsoft Docs Sign up for Azure DevOps tutorial. With an Azure DevOps, an organization and project will also be required. And again, instead of repeating these steps let's refer to the following documentation: Quickstart: Create an organization or project collection

 

We will also use a Container Registry and this can be created in the Azure Portal in the Container Registry section:

 

 

One thing to note when creating the container registry, is the Admin user will be used in the example so the admin user should be enabled:

 

That is the basic setup so let's move to running the application in the local dev environment.

Running the application in Docker

With the source download navigate to the src folder using bash or powershell. The Dockerfile that we will use to build the project is indicated in the screenshot below:

 

The container can be built using the following command:

 

docker build -t helloazuredev -f Dockerfile .

 

 

Take a moment to make sure the docker image runs using the following command: 

 

docker run -it --rm helloazuredev

 

 

You should see the following output indicating the container is running and use ctrl-c to quit:

 

With the container running let's create the Azure DevOps pipeline.

Adding to DevOps:

For this example we will be using an external source, a GitHub repo, to push a new docker container to an Azure Container Registry (ACR).

Let's start by creating a new pipeline in the Azure DevOps project by first clicking on the Builds menu:

 

For a blank project, you will see the New pipeline button available:

This starts a wizard, and the first step is to specify where the code will be located:

 

Depending on what projects you have been involved with on GitHub, you will be able to select the project from a collection of repos returned by GitHub:

 

If you are using a public repository, then you will need to take an additional step to approve the installation of Azure Pipelines:

 

The next step is to configure the pipeline, we will pick thee Docker pipeline to start with:

 

 

The only setting required is to identify the Dockerfile to use, and in this case, we are looking for any Dockerfile within the project:

Go ahead and Save and run this pipeline:

 

If all goes well the the pipeline should complete without any errors:

 

Next let's make a connection between the Azure DevOps project and the Azure Container Registry.

Pushing to an Azure Container Registry

The image is getting built without error but for this scenario we want to push the image to ACR. In order to do this we need to connect our ACR with the Azure DevOps project. This is done by adding a new Service connection. 

 

Under project settings (lower left corner), select Service connections:

The in the New service connection drop down, select Docker Registry:

This will allow us to specify container registries in Docker Hub, Azure Container Registry and others. We will use it to connect to our ACR:

Build and Push to ACR

Once that has been setup, let's go back to our pipeline and edit it so it pushes to our new service connection. There are many ways to do this so let's illustrate an interesting feature.

 

If we navigate to our source in GitHub, we will see a new file has been added: 

 

The azure-pipelines.yml file can be selected and edited. In our situation, let's modify the task to instead perform a build and push operation. The source is shown below and take special care with indentation as indentation is significant in YAML:

 

 

 

    - task: Docker@2
      displayName: Push HelloAzureDev
      inputs:
        containerRegistry: 'TechCommunityRegistry'
        repository: 'HelloAzureDev'
        command: 'buildAndPush'
        dockerfile: '**/Dockerfile'
        tags: |
          $(tag)

 

 

 

 

Once the file is save, a new build will be automatically started as by default a trigger is created to start whenever a commit is performed against the master branch. This is shown in the YAML file:

 

 

trigger:
- master

 

 

 

Checking the Registry

The final step is to confirm that a new image has been pushed to ACR. In the Azure Portal, select the Azure Container Registry and select the Repositories section:

 

If all completed without error, then there will be a new helloazuredev repository with a tagged image as illustrated below:

Note the tag corresponds to the build id from the Azure DevOps pipeline.

Summary

This article provides an example of using Azure DevOps to build and push a docker image to an Azure Container Registry. This provides many advantages including a consistent approach to producing container images and automating the build process.

 

 

Updated Oct 29, 2019
Version 2.0
No CommentsBe the first to comment