Skip to main content

Deploying Docker to Azure App Service

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

์ด ๋ฌธ์„œ์˜ ๋‚ด์šฉ

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 ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

    • ์ธ์ฆ์€ Azure CLI๋กœ ๋กœ๊ทธ์ธ์„ ์ฐธ์กฐํ•˜์„ธ์š”.
    • ์ƒˆ ๋ฆฌ์†Œ์Šค ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ az group์„ ์ฐธ์กฐํ•˜์„ธ์š”.
  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์ด๋ผ๋Š” ๋น„๋ฐ€์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋น„๋ฐ€์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ 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 ์›Œํฌํ”Œ๋กœ๊ฐ€ ํ™˜๊ฒฝ์— ๋ฐฐํฌ๋˜๋ฉด ํ™˜๊ฒฝ์ด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ธฐ๋ณธ ํŽ˜์ด์ง€์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์Šน์ธ์„ ์š”๊ตฌํ•˜๊ฑฐ๋‚˜ ์›Œํฌํ”Œ๋กœ, ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐฐํฌ ๋ณดํ˜ธ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒŒ์ดํŠธ ๋ฐฐํฌ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„๊ธฐ๋ฅผ ์ œํ•œํ•˜๊ฑฐ๋‚˜ ๋น„๋ฐ€์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฐฐํฌ ํ™˜๊ฒฝ ๊ด€๋ฆฌ์„(๋ฅผ) ์ฐธ์กฐํ•˜์„ธ์š”.

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 ๊ฐ’์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ 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 }}'

Further reading