You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dockerfile/Compose配置5000端口转发?Azure部署超时排查

Flask应用部署Azure Web Apps超时问题排查与修复

问题核心

Azure Web Apps for Containers 会将外部请求转发到容器的指定端口(默认8080),若容器监听端口与Azure预期不一致,或未正确配置端口映射/环境变量,就会触发超时。你本地运行正常是因为手动指定了-p 5000:5000做端口转发,但Azure环境中缺少对应配置。

解决方案

场景1:单容器部署(移除Nginx,直接用Gunicorn对外)

Azure自带反向代理,无需额外Nginx,简化配置更易排查:

  1. 修改Dockerfile,添加Azure识别的PORT环境变量:
FROM python:3.9-buster

ENV PORT=5000
EXPOSE $PORT

COPY requirements.txt .

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["gunicorn", "--config", "gunicorn-cfg.py", "run:app"]
  1. 确认gunicorn配置(你当前的配置已符合要求):
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""

bind = '0.0.0.0:5000'
workers = 1
accesslog = '-'
loglevel = 'debug'
capture_output = True
enable_stdio_inheritance = True
  1. 部署时,在Azure Web Apps的配置 > 应用程序设置中添加PORT=5000(若Dockerfile已设置ENV,此步可省略,但手动设置更稳妥)。

场景2:多容器部署(保留Nginx)

若必须使用Nginx做反向代理,需调整以下配置:

  1. 修改docker-compose.yml,添加环境变量并标记web容器:
version: '3.8'
services:
  appseed-app:
    container_name: appseed_app
    restart: always
    env_file: .env
    build: .
    networks:
      - db_network
      - web_network
    environment:
      - PORT=5000

  nginx:
    container_name: nginx
    restart: always
    image: "nginx:latest"
    ports:
      - "5000:5000"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    networks:
      - web_network
    depends_on: 
      - appseed-app
    labels:
      - "com.microsoft.azure.appservice.webserver.port=5000"

networks:
  db_network:
    driver: bridge
  web_network:
    driver: bridge
  1. 更新Nginx配置(./nginx/default.conf),确保监听5000端口并反向代理到应用服务:
server {
    listen 5000;
    server_name localhost;

    location / {
        proxy_pass http://appseed-app:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  1. 部署时,在Azure Web Apps的容器设置中选择多容器模式,上传修改后的docker-compose.yml,并在应用程序设置中添加PORT=5000。

额外排查建议

  • 查看Azure Web Apps的日志流,确认容器启动状态及Gunicorn/Nginx是否有报错
  • 通过Azure容器SSH功能,进入容器执行curl http://localhost:5000/index.html,验证应用在容器内是否可访问

内容的提问来源于stack exchange,提问作者Phil Robinson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 08:01:24