我正在使用简单的 docker-compose 设置来学习 azure 和 github ci/cd,该设置在我的开发计算机上完美运行,但在 Azure WebApp 上不断失败,并出现以下错误:
2024-09-04T16:12:01.352714815Z The command could not be loaded, possibly because:
2024-09-04T16:12:01.352738315Z * You intended to execute a .NET application:
2024-09-04T16:12:01.352775915Z The application 'foo.dll' does not exist.
2024-09-04T16:12:01.352781015Z * You intended to execute a .NET SDK command:
2024-09-04T16:12:01.352784615Z No .NET SDKs were found.
2024-09-04T16:12:01.352787915Z
2024-09-04T16:12:01.352790915Z Download a .NET SDK:
2024-09-04T16:12:01.352794015Z https://aka.ms/dotnet/download
2024-09-04T16:12:01.352797215Z
2024-09-04T16:12:01.352800215Z Learn about SDK resolution:
2024-09-04T16:12:01.352806415Z https://aka.ms/dotnet/sdk-not-found
在进一步检查部署中心/容器日志中的日志时,我发现 WebApp 内部甚至没有调用 docker-compose up 来启动容器,这些容器在我的开发计算机上运行得很好,而是运行以下命令:
2024-09-04T16:20:41.519Z INFO - Starting multi-container app..
2024-09-04T16:20:42.049Z INFO - Pulling image: bar.azurecr.io/web:latest
2024-09-04T16:20:42.218Z INFO - latest Pulling from web
2024-09-04T16:20:42.265Z INFO - Digest: sha256:<redacted>
2024-09-04T16:20:42.265Z INFO - Status: Image is up to date for bar.azurecr.io/web:latest
2024-09-04T16:20:42.279Z INFO - Pull Image successful, Time taken: 0 Seconds
2024-09-04T16:20:42.339Z INFO - Starting container for site
2024-09-04T16:20:42.340Z INFO - docker run -d -p 6733:5104 --name foo_web-app_0_871e06a6 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=foo -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=foo.azurewebsites.net -e WEBSITE_INSTANCE_ID=<some_id> bar.azurecr.io/web:latest
2024-09-04T16:20:42.341Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-09-04T16:20:42.887Z INFO - Pulling image: bar.azurecr.io/db:latest
2024-09-04T16:20:43.082Z INFO - latest Pulling from db
2024-09-04T16:20:43.141Z INFO - Digest: sha256:<redacted>
2024-09-04T16:20:43.142Z INFO - Status: Image is up to date for bar.azurecr.io/db:latest
2024-09-04T16:20:43.154Z INFO - Pull Image successful, Time taken: 0 Seconds
2024-09-04T16:20:43.212Z INFO - Starting container for site
2024-09-04T16:20:43.213Z INFO - docker run -d --expose=1433 --name foo_db_0_871e06a6 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=foo -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=foo.azurewebsites.net -e WEBSITE_INSTANCE_ID=<some_id> bar.azurecr.io/db:latest
2024-09-04T16:20:43.214Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
我的示例 Docker-Compose 文件
name: sample-app
services:
web-app:
image: bar.azurecr.io/web:latest
environment:
- ConnectionStrings__DefaultConnection=Server=db;Database=${DbName};User Id=${UserName};Password=${Password};MultipleActiveResultSets=true; TrustServerCertificate=true
ports:
- "80:5104"
volumes:
- ${WEBAPP_STORAGE_HOME}/app_data:/app
networks:
- isolated_network
depends_on:
- db
db:
image: bar.azurecr.io/db:latest
ports:
- "1433:1433"
networks:
- isolated_network
volumes:
- ${WEBAPP_STORAGE_HOME}/db_data:/var/opt/mssql
networks:
isolated_network:
driver: bridge
我的 GitHub 工作流程文件:
name: Build and deploy ASP.Net Core app to an Azure Web App
env:
ACR_NAME: bar
AZURE_WEBAPP_NAME: foo
AZURE_WEBAPP_PACKAGE_PATH: '.'
on:
push:
branches: [ "main" ]
workflow_dispatch:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Log in to Azure Container Registry
run: |
az acr login -n ${{ env.ACR_NAME }}
- name: Build and push Web image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ env.ACR_NAME }}.azurecr.io/web:latest
- name: Build and push Db image
uses: docker/build-push-action@v2
with:
context: .
file: ./DockerDbfile
push: true
tags: ${{ env.ACR_NAME }}.azurecr.io/db:latest
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
configuration-file: 'docker-compose.yml'
我的问题是:
我使用 docker compose 创建了一个示例多容器应用程序,并通过 GitHub 部署到 ACR。
我已引用此doc并创建了应用程序。
感谢 @alaintd 的清晰解释,我已参考此 doc 使用 GitHub 操作构建 Docker 映像并将其推送到 Azure 容器注册表。
确保在 GitHub 存储库设置 -> 机密和变量 -> 操作 -> 新存储库机密中添加 Azure 容器注册表凭据。
我已将以下代码行添加到我的工作流程文件中以登录 Azure 容器注册表。
name: Azure Container Registry Login
uses: Azure/docker-login@v1
with:
login-server: multiappregis.azurecr.io
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
我完整的工作流程文件:
name: Build and Push Docker Images
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Azure Container Registry Login
uses: Azure/docker-login@v1
with:
login-server: multiappregis.azurecr.io
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Build and push the first image (app)
uses: docker/build-push-action@v2
with:
context: ./multiapp
file: ./multiapp/Dockerfile
push: true
tags: <ACR-NAME>.azurecr.io/app:latest
- name: Build and push the second image (db)
uses: docker/build-push-action@v2
with:
context: ./multiapp
file: ./multiapp/Dockerdb/Dockerfile
push: true
tags: <ACR-NAME>.azurecr.io/db:latest
输出: