Configure CI/CD in Azure Pipelines to deploy docker containers as Azure Web App

Few days back, we learned about how to publish Azure Container Instances where-in we can deploy either a container or group of containers and use the same. Azure Web App for Containers allows you to not only run your containers but it also brings forth the PaaS innovations for the Web App. So it brings best of the both worlds together. It also allows you to not worry about the maintaining an container orchestrator mechanism. You can prefer to package their code and dependencies into containers using various CI/CD systems like Jenkins, Maven, Travis CI or VSTS, alongside setting up continuous deployment web hooks with App Service.

In this blog post we’ll learn more about how to deploy .NETCore application packaged as docker container and using CI/CD in Azure Pipelines (Formerly VSTS).

Below is one of the very simple possible architecture pattern for this configuration:

architecture pattern for azure web app for containers

Create an Azure Resource Group

We can easily create a resource group using either Azure CLI or using Az CLI commands in the cloud shell. For the demo, I’ll be using the AZ CLI commands installed on my machine. The following example creates a resource group named webapp-rg in the West Europe location:

az group create –name webapp-rg –location “West Europe”

Create resource group using azure cli commands

To see all supported locations for App Service on Linux in Basic tier, run the below command:

az appservice list-locations –sku B1 –linux-workers-enabled

Create an Azure App Service Plan

Azure Web App for Containers is only supported by Azure App Service for Linux a of now. So we first we need to create the App Service Plan on Linux to proceed further. The following example creates an App Service plan named webapp-plan in the Basic pricing tier (–sku B1) and in a Linux container (–is-linux).

az appservice plan create –name webapp-plan –resource-group webapp-rg –sku B1 –is-linux

create app service plan using azure cli commands - 2

Create an Azure Web App

We now need to create an Azure Web App to host containers. When creating web app, we also need to define certain properties like runtime. The following example creates an web app with runtime as dotnet core 2.0:

az webapp create –resource-group webapp-rg –plan webapp-plan –name mgoyal –runtime “dotnetcore|2.0” –deployment-local-git

create azure web app with azure cli command

For some reason, above command would not work on the PowerShell. So I had to switch to command prompt. However, it works fine on the bash. You may also choose to create web app using Azure Portal.

Configure Azure Web App to use images from ACR (or any private registry)

We need to modify properties of web app and specify the deployment from docker registry where we have hosted our container. For this we can use below command:

az webapp config container set –name mgoyal –resource-group webapp-rg –docker-custom-image-name –docker-registry-server-url –docker-registry-server-user generic001 –docker-registry-server-password klldkdfdsdfswewdadd

Set container settings for the web app

where –docker-custom-image-name is the name of the docker image, –docker-registry-server is the location of registry, –docker-registry-server-user is the username for registry and  –docker-registry-server-password is the password for establishing connection with registry.

Next, switch to Azure Portal, go to web app you created and then go to container settings. Modify it to set continuous deployment as On:

provide values for the container images-2

Do note that I have chosen the Azure Container Registry, but as you can see, you can choose a registry provider of your choice.

Create a ASP.NET Core Web Application

For demo purpose, we have used the source code in one of the sample repositories provided by Microsoft at:

It contains the sample .NET Core code along with few test cases and dockerfile to convert it into a docker image.

Upload the source code in Azure Repos

Before, we can do the build and release using Azure Pipelines, we’ll need to upload source code to Azure Repos. For steps on how to upload source code to Azure Repos using Git, refer here.

You can also choose to directly import source code into Azure Repos from GitHub.

Configure Continuous Build Pipeline

For this, go to the required Project in Azure DevOps and go to Azure Pipelines section. From there, we can create new Build Pipeline:

create a new build pipeline

We can choose to configure pipeline using YAML provided in the source repository itself or define our tasks in old fashioned way. Next few steps contains configuration for manual steps in the build configuration.

For this, we first need to start with an empty job while creating new pipeline. Let’s select agent pool as ‘Hosted Ubuntu 1604’ and provide a name for the pipeline:

Provide values for the pipeline name and hosted pool

Since our Dockerfile is very bare, we’ll first need to add steps to build and publish .NET Core image. Let’s go to add tasks and add 3 tasks for .NET Core: Build, Test and Publish.

For the Build task, provide values as below:

dotnet core build task configuration

For the Test task, provide values as below:

dotnet core test task configuration

For the Publish task, provide values as below:

dotnet core publish task configuration

We now need to add couple of tasks for docker build and push. Again, go to the list of tasks and add docker task twice: One for building image and second to publish image.

Configure docker build task as below:

configure task to build docker image

Thereafter, configure docker publish task as below:

configure task to publish docker image

You may choose to not use Azure Container Registry (ACR) and use a provider registry of your choice.

Verify the flow works as expected

Once everything is configured, run the build pipeline, and it should get completed successfully:

Build Pipeline result summary for one of the test runs

Once build is complete, we should now be able to see the docker image created in the specified registry and repository:

view images stored in the azure container registry

We can now go to web app, copy the DNS name shown and open it in browser of our choice:

check web app after deploying containers

We can also check the container deployment logs if we want to know how our container is behaving. It is available at the same place as container settings:

docker logs for the containers in the container settings

As you can see, every time the build is complete, web app will be notified and the container is fetched and deployed again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s