Dave Rendón Microsoft Azure MVP, embracing and fostering tech intensity to benefit society and thrive in a digital world.

Azure DevOps: Automate your release pipeline to provision a Docker container to Azure Web App for containers

3 min read

Azure DevOps Automate your release pipeline to provision a Docker container to Azure Web App for containers- WikiAzure

On this article I´ll show you how to setup Azure DevOps to automate your release pipeline to provision a Docker container to Azure Web App for containers.

Objective: To be able to quickly containerize your node js app and deploy it to an Azure Container Registry and automatically deploy the container to an Azure web app for containers.

Azure DevOps Automate your release pipeline to provision a Docker container to Azure Web App for containers- WikiAzure

Prerequisites

  •  Azure subscription ID
  • Azure Subscription Name
  •  Resource Group
  •  Git installed

Preparing your resources

You need to create:

  1. Create Azure Container Registry
  2. Create Web App for Containers

Create Azure Container Registry

Go to the Azure Portal and create a new resource and look for container registry:

01-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Select Container Registry then click create:

02-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
03-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the following parameters to create your Container Registry:

04-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Create Web App for Containers

Now lets create the web app for containers:

Create a new resource, look for web:

05-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

On the results page select the webapp for containers and click create:

06-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
07-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the following parameters to create your web app for containers:

08-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now will provide the parameters for the containerized app

09-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Once the validation process is done, click create:

010-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Setup Azure Devops

Create a new project in your Azure DevOps account. Sign in to dev.azure.com and create a new project:

011-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
012-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Create a service connection (Azure subscription level) 

To create a service connection: 

Navigate to Project Settings by clicking on ‘manage your services’

013-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Under Project Settings, select Service Connections -> New service connection and select create new service connection

014-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

When the connection screen comes up, select Azure Resource Manager and click Next

015-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now select the option “Service Principal(automatic)” and click next:

016-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now select the scope level as “Subscription“, select your subscription and the resource group that you are going to work with. Then provide a name for your connection and ensure that you check the box to grant permission to all pipelines, then click save:

017-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Setup a repository in Azure DevOps

Go to repos and select import a repository:

018-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the repository url and click import:

019-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Once the process is done you should see the repository like below:

020-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Create Azure Devops BUILD pipeline: 

Azure Pipelines can be used to build images for any repository containing a Dockerfile. Building of both Linux and Windows containers is possible based on the agent platform used for the build.

Navigate to pipelines an click create pipeline

021-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now choose the source in this case Azure Repo:

021-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Select your azure repo:

022-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Select your azure repo:

023-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now on the “Configure your pipeline” pane, select Docker option to build and push an image to azure container registry:

024-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Select the subscription you are working with and click continue:

025-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now provide the parameters from the Azure Container Registry previously created and click on validate and configure:

026-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

This process will configure the resources needed and generate a YAML file.

Example of the yaml file:

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- master
resources:
- repo: self
variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'e24a474b-ad6b-44d6-860d-f5a32f500360'
  imageRepository: 'contososite'
  containerRegistry: 'contoso.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/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)

Now click on save and run:

028-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the description as needed and click save and run

029-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

This will create a build job:

030-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
031-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Once the job is completed you can go to your Azure Container Registry and should be able to see the repository added:

032-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Create Azure Devops RELEASE pipeline:

Now go back to pipelines, select releases and create a new release pipeline:

033-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Select the Azure App Service Deployment template and choose Apply

034-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You will be prompted to similar screen as below, close this dialog:

035-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now click on tasks and select stage 1:

036-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now click on unlink all and confirm:

037-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now select the deploy Azure App service:

038-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the parameters to provision the app service:

039-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Once completed click save at the very top of the page:

040-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Then click OK

041-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Create a release to deploy your app

We´re now ready to create a release, which means to run the release pipeline with the artifacts produced by a specific build. This will result in deploying the build.

Choose Create release:

042-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now check that the artifact version you want to use is selected and select Create.

043-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You should see a notification like below:

044-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You can check the status and logs by clicking on the Release-1:

045-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
046-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

After the release is complete, navigate to your site running in Azure using the Web App URL http://{web_app_name}.azurewebsites.net, in this case: https://contoso-app.azurewebsites.net

047-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Go to your app settings and ensure you have the following configuration:

048-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You can also check on the logs to verify that the container is up and running:

049-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Automate the Azure Devops release pipeline

Now this prepared our next release but we still need to automate the release pipeline, go to releases and click edit:

050-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Click on Add an artifact:

051-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Provide the following parameters:

052-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Then click Save.

Now on the pipeline, select the lightning icon and enable a continuous deployment trigger as shown below – will enable just #2-continuous deployment trigger:

053-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now click on the pre-deployment conditions:

054-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Configure the pre-deployment conditions as below:

055-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You can go to your azure repo, perform a change on the index file and commit the change:

056-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

This will trigger the pipeline:

057-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

You should see the new release :

058-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure

Now when you perform any changes to the application, you will be able quickly containerize your node js app and deploy it to a container registry and will be automatically deployed to the Azure web app for containers:

059-Azure DevOps - Automate your release pipeline to provision Docker containers to Azure App Service WikiAzure
Dave Rendón Microsoft Azure MVP, embracing and fostering tech intensity to benefit society and thrive in a digital world.

4 Replies to “Azure DevOps: Automate your release pipeline to provision a…”

  1. The release pipeline takes the docker image from the repository ? why the trigger to launch it depends on git ? Is not better to depend on the azure repository container ?

    1. Hi Martinib77,

      Thanks much for taking the time to review this blog post, the initial approach use Docker hub and then ACR, thank you! 👍

  2. Very good article, Thanks Dave.

    One question, If I want to deploy to a Web App for Windows, is this possible? I can only see Web App for Containers (Linux)

    1. Hi Tommy,

      Thanks much for taking the time to read it, it is possible to deploy a webapp for containers(Windows), on this blog post I´m referring to Linux but you can also try Windows, this article might help you – http://bit.ly/az-windows-containers

      Let me know your thoughts,
      thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *