Deploy an Azure App custom container

You can use App Service on Linux and pipelines to automatically deploy your web app to a custom container in Azure.

Azure App Service is a managed environment for hosting web applications, REST APIs, and mobile back ends. You can develop in your favorite languages, including .NET, Python, and JavaScript.

You'll use the Azure Web App for Container task to deploy to Azure App Service in your pipeline.

To learn how to deploy to an Azure Web App without a container, see Deploy an Azure Web App.

Note

This guidance applies to Team Foundation Server (TFS) version 2017.3 and later.

Prerequisites

Build and push your image

Azure Pipelines can be used to push images to container registries such as Azure Container Registry (ACR), Docker Hub, Google Container Registries, and others. This example pushes an image to Azure Container Registry.

  1. Sign in to your Azure DevOps organization and navigate to your project.

  2. Go to Pipelines, and then select New Pipeline.

  3. Select GitHub as the location of your source code and select your repository.

    Note

    You might be redirected to GitHub to sign in. If so, enter your GitHub credentials. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve and install.

  4. From the Configure tab, select the Docker - Build and push an image to Azure Container Registry task.

  5. Select your Azure Subscription, and then select your Container registry from the dropdown menu.

  6. Provide an Image Name to your container image, and then select Validate and configure.

    As Azure Pipelines creates your pipeline, it:

    • Creates a Docker registry service connection to enable your pipeline to push images to your container registry.

    • Generates an azure-pipelines.yml file, which defines your pipeline.

  7. Save and run your pipeline. Your YAML pipeline should look similar to this:

- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build job
    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)

If you're an experienced pipeline user and already have a YAML pipeline to build your .NET Core app, then you might find the examples below useful.

YAML pipelines aren't available on TFS.

Now that the build pipeline is in place, you will learn a few more common configurations to customize the deployment of the Azure Container Web App.

Deploy with Azure Web App for Container

Deploy to an Azure App custom container with the Azure Web App for Container task.


trigger:
- master

resources:
- repo: self

variables: 
  ## Add this under variables section in the pipeline
  azureSubscription: <Name of the Azure subscription>
  appName: <Name of the Web App>
  containerRegistry: <Name of the Azure container registry>
  dockerRegistryServiceConnection: '4fa4efbc-59af-4c0b-8637-1d5bf7f268fc'
  imageRepository: <Name of image repository>
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

  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)


    ## Add the below snippet at the end of your pipeline
    - task: AzureWebAppContainer@1
      displayName: 'Azure Web App on Container Deploy'
      inputs:
        azureSubscription: $(azureSubscription)
        appName: $(appName)
        containers: $(containerRegistry)/$(imageRepository):$(tag)

The Azure Web App on Container task will pull the appropriate Docker image corresponding to the BuildId from the repository specified, and then deploy the image to your Azure App Service on Linux.

YAML pipelines aren't available on TFS.

Deploy to a slot

You can configure the Azure Web App container to have multiple slots. Slots allow you to safely deploy your app and test it before making it available to your customers.

The following YAML snippet shows how to deploy to a staging slot, and then swap to a production slot:

- task: AzureWebAppContainer@1
  inputs:
    azureSubscription: '<Azure service connection>'
    appName: '<Name of the web app>'
    containers: $(containerRegistry)/$(imageRepository):$(tag)
    deployToSlotOrASE: true
    resourceGroupName: '<Name of the resource group>'
    slotName: staging

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: '<Azure service connection>'
    WebAppName: '<name of web app>'
    ResourceGroupName: '<name of resource group>'
    SourceSlot: staging
    SwapWithProduction: true

YAML pipelines aren't available on TFS.

FAQ

How do I find my registry credentials for the web app?

App Service needs information about your registry and image to pull the private image. In the Azure portal, go to Container settings from the web app and update the Image source, Registry and save.

Screenshot showing Update image source and Registry in container settings.