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

执行sudo docker-compose up时Worker启动停滞,本地端口无法访问

排查Docker Compose中Gunicorn服务启动后无法访问的问题

现象

Docker Compose启动后,Gunicorn日志显示3个worker已完成启动,但本地访问映射端口8032时提示"无法访问此网站"。已尝试调整Gunicorn超时参数、增加Docker内存限制,问题未解决。

现有配置

Dockerfile

FROM docker.io/python:3.10

WORKDIR /

RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y python3 python3-pip git
COPY . /

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

ENV GUNICORN_CMD_ARGS="--workers=3 --bind=0.0.0.0:8080 --timeout=1000"

EXPOSE 8080

CMD [ "gunicorn", "main:app" ]

docker-compose.yml

version: '3'
services:
        web:
                image: runtime_t_v1
                build: .
                ports:
                        - "8032:8080"
                volumes:
                        - ./volumes:/volumes
                        - ./instance:/instance
                restart: unless-stopped

启动日志

(base) Theo@Theo runtime_flask % sudo docker-compose up
[+] Running 1/0
 ⠿ Container runtime_flask-web-1  Created                                                                                       0.0s
Attaching to runtime_flask-web-1
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [1] [INFO] Starting gunicorn 20.1.0
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [1] [INFO] Using worker: sync
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [7] [INFO] Booting worker with pid: 7
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [8] [INFO] Booting worker with pid: 8
runtime_flask-web-1  | [2023-04-07 20:23:34 +0000] [9] [INFO] Booting worker with pid: 9

排查步骤

1. 确认端口映射有效性

  • 执行docker ps,查看容器的PORTS列是否显示0.0.0.0:8032->8080/tcp,确认端口映射已正确创建
  • 用docker inspect runtime_flask-web-1 | grep -A 10 "Ports"查看更详细的端口绑定信息,确保没有绑定到特定IP而非0.0.0.0

2. 验证容器内部服务状态

  • 进入容器:docker exec -it runtime_flask-web-1 bash
  • 在容器内部执行curl http://localhost:8080,测试服务是否能正常响应:
    • 如果curl返回错误或超时,说明问题出在Flask应用本身,而非网络映射:
      • 检查main.py中是否存在启动时的阻塞操作(如加载大模型、数据库连接未完成)
      • 查看应用相关日志,比如instance或volumes目录下的日志文件,是否有初始化报错
      • 确认main:app中的app是正确的Flask实例,没有拼写错误或未正确初始化

3. 检查本地网络与端口占用

  • 执行lsof -i :8032(macOS/Linux)或netstat -ano | findstr :8032(Windows),确认8032端口未被其他进程占用
  • 临时关闭本地防火墙,或在防火墙规则中放行8032端口,排除防火墙拦截问题

4. 调整Gunicorn配置排查

  • 增加日志级别获取更多细节:修改GUNICORN_CMD_ARGS为"--workers=3 --bind=0.0.0.0:8080 --timeout=1000 --log-level=debug",重启容器查看是否有隐藏报错
  • 尝试更换worker类型:添加--worker-class=gevent到GUNICORN_CMD_ARGS,同时在requirements.txt中添加gevent,排除同步worker的阻塞问题

5. 检查应用初始化逻辑

  • 在Flask应用的初始化代码末尾添加日志输出(如print("App initialized successfully")),确认应用是否真的完成初始化
  • 如果应用依赖外部服务(如数据库、Redis),检查容器是否能正常访问这些服务,是否存在连接超时或认证失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 16:27:28