在使用 Azure DevOps 管道将 Python 应用程序部署到 Azure 应用服务时遇到问题。
尝试了 Docker 映像和 zip 部署,但导致了相同的错误。
应用程序服务计划是Linux。部署成功,但我在浏览器中收到网关超时“504.0 Gateway Timeout”。
网关错误:
Python 应用程序在本地运行良好,我可以使用 VS Code 发布到 Azure 应用服务。
通过添加 WEBSITES_PORT 键更新了应用程序设置,并尝试使用 8000、8080 和 5000 端口号,但没有成功。
来自日志流的日志:
这是 docker 文件:
根据到目前为止的描述,当我的应用程序容器无法运行/启动时,我只能重现类似的问题。可能有许多不同的错误导致容器启动失败,而管道运行可能会成功,因为它只是设置配置但没有等待容器启动。
您可以利用应用程序高级工具检查容器日志并找出实际错误。
这是我根据本文档中的简单演示更新的示例,以及我的 YAML 管道定义,供您参考。
更新了 Dockerfile
# Pull a pre-built alpine docker image with nginx and python3 installed
FROM tiangolo/uwsgi-nginx:python3.8-alpine-2020-12-19
# Set the port on which the app runs; make both values the same.
#
# IMPORTANT: When deploying to Azure App Service, go to the App Service on the Azure
# portal, navigate to the Applications Settings blade, and create a setting named
# WEBSITES_PORT with a value that matches the port here (the Azure default is 80).
# You can also create a setting through the App Service Extension in VS Code.
ENV LISTEN_PORT=5000
EXPOSE 5000
# Indicate where uwsgi.ini lives
ENV UWSGI_INI uwsgi.ini
# Tell nginx where static files live. Typically, developers place static files for
# multiple apps in a shared folder, but for the purposes here we can use the one
# app's folder. Note that when multiple apps share a folder, you should create subfolders
# with the same name as the app underneath "static" so there aren't any collisions
# when all those static files are collected together.
ENV STATIC_URL /hello_app/static
# Set the folder where uwsgi looks for the app
WORKDIR /hello_app
# Copy the app contents to the image
COPY . /hello_app
# If you have additional requirements beyond Flask (which is included in the
# base image), generate a requirements.txt file with pip freeze and uncomment
# the next three lines.
COPY requirements.txt /
RUN pip install --no-cache-dir -U pip
RUN pip install --no-cache-dir -r /requirements.txt
azure-pipelines.yml
trigger:
- none
pool:
vmImage: ubuntu-latest
variables:
# ACR
dockerRegistryServiceConnection: 'ACRSvcCnnWIF'
imageRepository: 'containerapp/python/flask'
containerRegistry: 'azacrxxxxxx.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: $(Build.BuildId)
# Azure Web App for container
ARMSvcCnn: ARMSvcCnnWIFAutoSub1
webAppName: azwebapp-xxxxxx-python-container
stages:
- stage: CI_BuildPushACRImage
jobs:
- job: BuildPushACRImage
displayName: Build
steps:
- task: Docker@2
displayName: Build and push an image to Azure Container Registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- stage: CD_DeployAzureWebAppContainer
dependsOn: CI_BuildPushACRImage
jobs:
- deployment: DeployAzureWebAppContainer
environment: E-AzureWebAppContainer
strategy:
runOnce:
deploy:
steps:
- task: AzureWebAppContainer@1
inputs:
azureSubscription: '$(ARMSvcCnn)'
appName: '$(webAppName)'
containers: '$(containerRegistry)/$(imageRepository):$(tag)'
appSettings: >
-DOCKER_REGISTRY_SERVER_PASSWORD $(DOCKER_REGISTRY_SERVER_PASSWORD)
-DOCKER_REGISTRY_SERVER_USERNAME $(DOCKER_REGISTRY_SERVER_USERNAME)
-DOCKER_REGISTRY_SERVER_URL https://$(containerRegistry)
-WEBSITES_PORT 5000