Azure Container Registry (ACR) is a fully managed private Docker registry in Azure. In this post, I will show you how to create a continuous integration pipeline in Visual Studio Team Services to build a Docker image and push it to Azure Container Registry.
If you’re not familiar with Docker, containers or Azure Container Registry I’ve included some resources for you take a look:
This post will use an Azure sample application on GitHub.
Before you begin
Before you can complete the steps in this tutorial you will need:
- A Visual Studio Team Services account. You can create one for free here.
- An Azure Container Registry. You can see how to get started here.
Create a project
Let’s get started by creating a new project in VSTS. You can give the project a name of your choice, but for the purpose of this tutorial make sure you select Git for Version control.
After creating a project, VSTS will redirect you to ‘Get started with your new project’. We can use this tool to import Azure-Samples/aci-helloworld into a new VSTS repository. From the ‘Get started’ page of your newly created project select import a repository and Import. Complete the form with the following details:
- Source type: Git
- Clone URL: https://github.com/Azure-Samples/aci-helloworld.git
This shouldn’t take more than a few seconds. When complete, you will be redirected to your newly created private Git repository. From here you can browse the sample code. Take a look at the Dockerfile and explore the app directory.
Create a build
Once you are familiar with the project select Set up build. VSTS has a pre-built template that we can use to build an image and push to Azure Container Registry. Search for ‘Container’ and select Apply.
Before we take a look at the steps to build and push an image, update the Agent queue to ‘Hosted Linux Preview’. You’ll find this option by selecting Process on the left-hand side.
Build an image
The first step uses the Docker task. This task can be used to build, tag, push, run Docker images, or run a Docker command. It can be used with Docker or Azure Container Registry. Most of the options are pre-populated, but we need to make some changes:
- Container Registry Type: Azure Container Registry
- Azure subscription: Select the same subscription as your Azure Container Registry (you may need to authorise this connection)
- Azure Container Registry: Select your Azure Container Registry
- Image Name: aci-tutorial-app:$(Build.BuildNumber)
- Qualify Image Name: Checked
The image name uses the build number variable to tag (version) your container image. This auto-increments every time a build is run.
Qualify Image Name will prepend your registry’s hostname to your image tag - this is required to be able to push your image to a registry.
Push an image
The second step in the build will push your image to a container registry. In a traditional build pipeline we would publish build artifacts to VSTS. These would later be deployed by a release pipeline. In our case, because we are using Docker, we need to push the image to a container registry. Later, a release pipeline could pull the image from the registry.
As per the first step, we need to make some changes:
- Container Registry Type: Azure Container Registry
- Azure subscription: Select the same subscription as Step 1.
- Azure Container Registry: Select the same Azure Container Registry as Step 1.
- Image Name: aci-tutorial-app:$(Build.BuildNumber)
- Qualify Image Name: Checked, as per Step 1.
That’s all the required configuration. Select Save & queue and VSTS will queue your newly created build. You can navigate to your build to get progress updates and a summary once complete. Hopefully, it’s successful.
The build log should confirm that VSTS has pushed an image to your registry with the following tag:
<registry>.azurecr.io/aci-tutorial-app:<tag>
Log into Azure, and open your Container Registry. Select Repositories from the navigation blade and you will find a copy of the image in your registry.
Now, every time your VSTS build is triggered a new Docker image will be built, tagged and pushed to your Azure Container Registry.
In just a few steps I have demonstrated how you can use a VSTS build pipeline to automate and streamline publishing Docker images to your private registry. Using Container Registry and an automated build process you can simplify container deployments for DC/OS, Docker Swarm, Kubernetes, and Azure services such as App Service, Service Fabric, Container Instances and more.
In this example we used a sample application. To help you understand the build process further you could modify the sample project or introduce your own Dockerfile and application.
I hope you have found this short tutorial helpful.