Skip to content

Bug: Docker 容器启动失败 - xvfb-run 启动命令问题 #2

Description

@LupinLin1

Bug Report: Docker 容器启动失败 - xvfb-run 启动命令问题

🐛 Bug 描述

使用最新镜像 ghcr.io/lupinlin1/flow2api:latest 启动容器时,应用无法正常运行。容器显示为 running 状态,但 API 无响应,日志为空。

🔍 复现步骤

  1. 拉取最新镜像:docker pull ghcr.io/lupinlin1/flow2api:latest
  2. 使用 docker-compose 启动:
services:
  flow2api:
    image: ghcr.io/lupinlin1/flow2api:latest
    ports:
      - "38000:8000"
  1. 容器启动成功,但 curl http://localhost:38000/docs 返回空响应

📊 实际行为

$ docker ps
CONTAINER   ghcr.io/lupinlin1/flow2api:latest   "xvfb-run -a '--serv…"   Up 1 minute

$ curl http://localhost:38000/docs
curl: (52) Empty reply from server

$ docker logs flow2api
(无输出)

✅ 预期行为

API 应该正常响应,/docs 端点应该返回 Swagger UI 页面

🔧 问题分析

检查容器进程发现:

  • PID 1 是 xvfb-run 进程
  • 没有找到 python main.py 子进程
  • 说明 xvfb-run 未能正确启动 Python 应用

手动在容器内运行 python main.py 可以正常启动:

INFO:     Started server process [82]
INFO:     Uvicorn running on http://0.0.0.0:8000

💡 临时解决方案

在 docker-compose.yml 中覆盖启动命令:

services:
  flow2api:
    image: ghcr.io/lupinlin1/flow2api:latest
    command: ["python", "main.py"]  # 覆盖 Dockerfile 中的 CMD

🛠️ 建议修复

方案 1:修改 Dockerfile 默认命令

# 当前(有问题)
CMD ["xvfb-run", "-a", "--server-args=-screen 0 1280x720x24", "python", "main.py"]

# 建议:直接启动,让用户按需启用 Xvfb
CMD ["python", "main.py"]

方案 2:通过环境变量控制

ENV ENABLE_XVFB=false
CMD ["sh", "-c", "if [ \"$ENABLE_XVFB\" = \"true\" ]; then xvfb-run -a --server-args=-screen 0 1280x720x24 python main.py; else python main.py; fi"]

方案 3:提供多个镜像标签

  • ghcr.io/lupinlin1/flow2api:latest - 不带 Xvfb
  • ghcr.io/lupinlin1/flow2api:with-xvfb - 带 Xvfb

📋 环境信息

  • Docker 版本: 24.0.x
  • 镜像: ghcr.io/lupinlin1/flow2api:latest (digest: 0b22bf0161f1)
  • 提交: e5c4bba (包含 Xvfb 支持)
  • 基础镜像: python:3.11-slim
  • 主机系统: Linux 5.15

🔗 相关提交

这个问题是在以下提交后引入的:

  • 98a69a0 feat: 添加 Xvfb 虚拟显示支持和 GitHub Actions CI/CD
  • Dockerfile 变更: CMD ["python", "main.py"]CMD ["xvfb-run", ...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocker

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions