Automate the building and deployment of a Docker image in Azure

Adding automatic tasks to the deployment cycle

Eduardo Vioque
3 min readMay 12, 2020

RECAP: In this prior post, I explained how to deploy a Dash application in Azure using Docker. I used the Azure container registry to register the Docker images and the Azure service Web App to deploy the Dash application. We did the whole process once and manually.

In the current post we aim to automate:

  1. The process of building our Docker image in the container registry when a commit in the master branch of our application code is pushed to a GitHub repository.
  2. The deployment of that image as a Web Application.
Updating our repository automatically triggers building a new Docker image with the new code, and deploying that new image as a Web App.

I will assume we have an Azure Container Registry and a GitHub repository for our Dash application code.

Build a Docker Image on source code changes

Azure container registry allows us to trigger tasks on certain events. In this example, we will automatically build our Docker image in the container registry, every time we update the source code of our application in a Github repository.

We first need a GitHub personal access token (PAT). Azure will use this token to access our repository and look for updates in the application source code. To get it, navigate to https://github.com/settings/tokens/new and create a token as follows:

Note: If you are accessing a private repository you will need to select the option “full control of the private repositories”.

Create a task using the Azure CLI

Adapt the code below to create a task with the Azure CLI:

ACR_NAME=<registry-name>        # Azure Container registry
GIT_USER=<github-username> # GitHub username
GIT_REPO=<github-repository> # GitHub repository name
GIT_PAT=<personal-access-token> # Git PAT generated
IMAGE_NAME=<docker_image_name> # Name of the image
az acr task create \
--registry $ACR_NAME \
--name build_on_commit \
--image $IMAGE_NAME:prod \
--context https://github.com/$GIT_USER/$GIT_REPO \
--file Dockerfile \
--git-access-token $GIT_PAT

Once the task is created you should see it in the Azure Portal:

We can trigger the task manually with:

az acr task run --registry $ACR_NAME --name build_on_commit

Or we can push a change to our GitHub repository to trigger the task automatically.

You can list every execution of a task with the following command:

az acr task list-runs --registry $ACR_NAME --output table

Congratulations! We have a task that automatically rebuilds the Docker image in our container registry every time a commit is pushed to the master branch of our GitHub repository.

Automatically deploy the new Docker image in a Web App

In the previous section, we have seen how to create a task in our container registry to automatically rebuild a Docker image every time our application source code changes in the GitHub repository. Now we need our web app to use that new registered Docker image. In this section, we will see how to automatically refresh the image the web app is running.

All we have to do is to enable the “Continuous Deployment” option in the “Container Settings” section of the Web App and save the changes.

With this configuration, every time we update the selected image and tag in the container registry, the web app will be automatically updated pulling and running the new updated image.

Summary

This was an easy one! We’ve seen how to create tasks in a container registry to automate building a docker image every time a commit is pushed to a GitHub repository. We’ve also seen how to enable continuous deployment in an Azure Web App so it automatically pulls and runs new versions of a Docker image.

References

Tutorial on Azure Container Registry Tasks.

--

--