Skip to main content

Deploying Docker to Azure App Service

You can deploy a Docker container to Azure App Service as part of your continuous deployment (CD) workflows.

Introduction

This guide explains how to use GitHub Actions to build and deploy a Docker container to Azure App Service.

참고 항목

GitHub Actions 워크플로가 OIDC(OpenID Connect)를 지원하는 클라우드 공급자의 리소스에 액세스해야 하는 경우 클라우드 공급자에게 직접 인증하도록 워크플로를 구성할 수 있습니다. 이렇게 하면 이러한 자격 증명을 수명이 긴 비밀로 저장하지 않을 수 있고 다른 보안 이점을 제공할 수 있습니다. 자세한 내용은 OpenID Connect를 사용한 보안 강화 정보을(를) 참조하세요. and Configuring OpenID Connect in Azure.

Prerequisites

Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:

  1. Azure App Service 요금제를 만듭니다.

    예를 들어 Azure CLI를 사용하여 새 App Service 요금제를 만들 수 있습니다.

    Bash
    az appservice plan create \
       --resource-group MY_RESOURCE_GROUP \
       --name MY_APP_SERVICE_PLAN \
       --is-linux
    

    위의 명령에서 MY_RESOURCE_GROUP을 기존 Azure 리소스 그룹으로 바꾸고 MY_APP_SERVICE_PLAN을 App Service 요금제의 새 이름으로 바꿉니다.

    Azure CLI 사용에 대한 자세한 내용은 Azure 설명서를 참조하세요.

  2. Create a web app.

    For example, you can use the Azure CLI to create an Azure App Service web app:

    Shell
    az webapp create \
        --name MY_WEBAPP_NAME \
        --plan MY_APP_SERVICE_PLAN \
        --resource-group MY_RESOURCE_GROUP \
        --deployment-container-image-name nginx:latest
    

    In the command above, replace the parameters with your own values, where MY_WEBAPP_NAME is a new name for the web app.

  3. Azure 게시 프로필을 구성하고 AZURE_WEBAPP_PUBLISH_PROFILE 비밀을 만듭니다.

    게시 프로필을 사용하여 Azure 배포 자격 증명을 생성합니다. 자세한 내용은 Azure 설명서의 배포 자격 증명 생성을 참조하세요.

    GitHub 리포지토리에서 게시 프로필의 콘텐츠가 포함된 AZURE_WEBAPP_PUBLISH_PROFILE이라는 비밀을 만듭니다. 비밀을 만드는 방법의 자세한 내용은 Using secrets in GitHub Actions을(를) 참조하세요.

  4. Set registry credentials for your web app.

    Create a personal access token (classic) with the repo and read:packages scopes. For more information, see 개인용 액세스 토큰 관리.

    Set DOCKER_REGISTRY_SERVER_URL to http://ghcr.io, DOCKER_REGISTRY_SERVER_USERNAME to the GitHub username or organization that owns the repository, and DOCKER_REGISTRY_SERVER_PASSWORD to your personal access token from above. This will give your web app credentials so it can pull the container image after your workflow pushes a newly built image to the registry. You can do this with the following Azure CLI command:

     az webapp config appsettings set \
         --name MY_WEBAPP_NAME \
         --resource-group MY_RESOURCE_GROUP \
         --settings DOCKER_REGISTRY_SERVER_URL=http://ghcr.io DOCKER_REGISTRY_SERVER_USERNAME=MY_REPOSITORY_OWNER DOCKER_REGISTRY_SERVER_PASSWORD=MY_PERSONAL_ACCESS_TOKEN
    
  5. Optionally, configure a deployment environment. 환경은 일반적인 배포 대상(예: production, staging 또는 development)을 설명하는 데 사용됩니다. GitHub Actions 워크플로가 환경에 배포되면 환경이 리포지토리의 기본 페이지에 표시됩니다. 작업을 진행하기 위해 승인을 요구하거나 워크플로, 사용자 지정 배포 보호 규칙을 사용하여 게이트 배포를 트리거할 수 있는 분기를 제한하거나 비밀에 대한 액세스를 제한할 수 있습니다. 환경을 만드는 방법에 대한 자세한 내용은 Managing environments for deployment을(를) 참조하세요.

Creating the workflow

Once you've completed the prerequisites, you can proceed with creating the workflow.

The following example workflow demonstrates how to build and deploy a Docker container to Azure App Service when there is a push to the main branch.

Ensure that you set AZURE_WEBAPP_NAME in the workflow env key to the name of the web app you created.

배포 환경을 구성한 경우 환경의 이름으로 environment 값을 변경합니다. 환경을 구성하지 않은 경우 또는 워크플로가 프라이빗 리포지토리에 있고 GitHub Enterprise Cloud를 사용하지 않는 경우 environment 키를 삭제합니다.

YAML
# 이 워크플로는 GitHub에서 인증되지 않은 작업을 사용합니다.
# 작업은 타사에서 제공하며
# 별도의 서비스 약관, 개인정보처리방침, 지원 설명서에서 규정됩니다.
# 참조하세요.

# 커밋 SHA에 작업을 고정하는 것이 좋습니다.
# 최신 버전을 얻으려면 SHA를 업데이트해야 합니다.
# 태그 또는 분기를 참조할 수도 있지만 경고 없이 작업이 변경될 수 있습니다.

name: Build and deploy a container to an Azure Web App

env:
  AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name

on:
  push:
    branches:
      - main

permissions:
  contents: 'read'
  packages: 'write'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b

      - name: Log in to GitHub container registry
        uses: docker/login-action@8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Lowercase the repo name
        run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

      - name: Build and push container image to registry
        uses: docker/build-push-action@9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f
        with:
          push: true
          tags: ghcr.io/${{ env.REPO }}:${{ github.sha }}
          file: ./Dockerfile

  deploy:
    runs-on: ubuntu-latest

    needs: build

    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Lowercase the repo name
        run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          images: 'ghcr.io/${{ env.REPO }}:${{ github.sha }}'

Additional resources

The following resources may also be useful: