Skip to content

Latest commit

 

History

History
276 lines (193 loc) · 10.5 KB

deploy-docker-webapp.md

File metadata and controls

276 lines (193 loc) · 10.5 KB
title description ms.assetid ms.topic ms.date monikerRange recommendations
Deploy a container to Azure App Service with Azure Pipelines
How to deploy containerized web apps to Azure App Service with Azure Pipelines
78815F3C-4347-4C8B-AB4B-F36FC0D41531
quickstart
11/08/2023
<= azure-devops
true

Deploy a custom container to Azure App Service with Azure Pipelines

[!INCLUDE version-lt-eq-azure-devops]

Using Azure Pipelines, you can build, test, and automatically deploy your web app to an Azure App Service Web App container on Linux. In this article, you will learn how to use YAML or Classic pipelines to:

[!div class="checklist"]

  • Build and publish a Docker image to Azure Container Registry
  • Create an Azure Web App
  • Deploy a container to Azure App Service
  • Deploy to deployment slots

Prerequisites

Get the code

Fork the following sample app at GitHub.

https://github.com/spring-guides/gs-spring-boot-docker.git
https://github.com/MicrosoftDocs/pipelines-javascript-docker
https://github.com/Microsoft/python-sample-vscode-flask-tutorial
https://github.com/MicrosoftDocs/pipelines-dotnet-core-docker

Build and publish a Docker image to Azure Container Registry

To complete this section successfully, you must have an Azure Container Registry. Refer to the prerequisites section for details.

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

  2. Select Pipelines, and then New Pipeline.

  3. Select GitHub when prompted for the location of your source code, and then select your repository.

  4. Select the Docker: build and push an image to Azure Container Registry pipeline template.

    :::image type="content" source="media/docker-template.png" alt-text="Select Docker pipeline template":::

  5. Select your Azure subscription, and then select Continue.

  6. Select your Container registry from the drop-down menu, and then select Validate and configure.

    :::image type="content" source="media/validate-and-configure.png" alt-text="Validate and configure Docker":::

  7. Review the pipeline YAML template, and then select Save and run to build and publish the Docker image to your Azure Container Registry.

    trigger:
    - main
    
    resources:
    - repo: self
    
    variables:
        # Container registry service connection established during pipeline creation
        dockerRegistryServiceConnection: '{{ containerRegistryConnection.Id }}'
        imageRepository: 'javascriptdocker'
        containerRegistry: 'sampleappcontainerregistry.azurecr.io'
        dockerfilePath: '$(Build.SourcesDirectory)/app/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)
  8. To view the published Docker image after your pipeline run has been completed, navigate to your container registry in Azure portal, and then select Repositories.

    :::image type="content" source="media/docker-image-in-azure-portal.png" alt-text="Docker image published to Azure Container Registry":::

  9. To deploy your image from the container registry, you must enable the admin user account. Navigate to your container registry in Azure portal, and select Access keys. Next, select the toggle button to Enable Admin user.

    :::image type="content" source="media/enable-admin-user.png" alt-text="Enable Admin user":::

Create a Web App

  1. Navigate to Azure portal.

  2. Select Create a resource > Containers, and then choose Web App for Containers.

    :::image type="content" source="media/create-web-app-container.png" alt-text="Create a web app for containers resource":::

  3. Enter a name for your new web app, and create a new Resource Group. Select Linux for the Operating System.

    :::image type="content" source="media/configure-web-app.png" alt-text="Configure the web app":::

  4. In the Pricing plans section, choose the F1 Free plan.

  5. Select Review and create. Review your configuration, and select Create when you are done.

Deploy to Web App for Containers

In this YAML, you build and push a Docker image to a container registry and then deploy it to Azure Web App for Containers. In the Build stage, you build and push a Docker image to an Azure Container Registry with the Docker@2 task. The AzureWebAppContainer@1 task deploys the image to Web App for Containers.

trigger:
- main

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)
  1. From within your project, select Pipelines, and then select Release.

  2. Select New pipeline to create a new release pipeline.

  3. Select the Azure App Service deployment template

    :::image type="content" source="media/app-service-template.png" alt-text="Azure App Service template":::

  4. Select Tasks, then Unlink all in stage 1 to unlink all the pipeline parameters.

    :::image type="content" source="media/unlink-parameters.png" alt-text="Unlink pipeline parameters":::

  5. Select the Deploy Azure App Service task, and fill out the required fields. Select Save when you are done.

    :::image type="content" source="media/deploy-task.png" alt-text="Deploy Azure App Service task":::

  6. Select Create release, and then choose Stage 1 from the dropdown menu. Select Create when you are done.

    :::image type="content" source="media/create-release.png" alt-text="Create a release pipeline":::

  7. Hover over Stage 1 in your pipeline, and select Deploy to queue and start the deployment.

    :::image type="content" source="media/deploy-docker-image.png" alt-text="Queue and deploy Docker image":::

  8. Your pipeline logs should look similar to the screenshot below.

    :::image type="content" source="media/pipeline-logs.png" lightbox="media/pipeline-logs.png" alt-text="Pipeline logs":::

  9. Navigate to your newly deployed web app to verify your deployment.

    :::image type="content" source="media/deployed-web-app.png" alt-text="Web app deployed. Hello World message":::


Deploy to a deployment 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. See Create staging environments for more details.

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

You can configure the Azure Web App for container to have multiple slots. Slots allow you to safely deploy your app and test it before making it available to your customers. Use the option Deploy to Slot in the Azure Web App Container task to specify the slot to deploy to. You can swap the slots by using the Azure App Service Manage task.


FAQ

Q: How can I find my Docker registry credentials?

A: Navigate to Azure portal, and then select your Web App for Containers. Select Configuration > Application settings and then click to show the value.

A screenshot showing how to find Docker registry credentials.

Related articles