# 使用官方 Python 基础镜像 (Debian 体系) FROM python:3.9-slim # 设置工作目录 WORKDIR /app # 防止 apt-get 安装时出现交互式弹窗(比如选择时区)导致卡死 ENV DEBIAN_FRONTEND=noninteractive # Debian 12 (Bookworm) 的 apt 源文件变成了 /etc/apt/sources.list.d/debian.sources # 这里替换源以加速下载,并安装必要的系统依赖 # 必须安装:Xvfb(虚拟屏幕), Chromium(浏览器核心), 中文字体(防乱码) RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true && \ apt-get update && \ apt-get install -y --no-install-recommends \ xvfb \ xauth \ chromium \ chromium-driver \ fonts-wqy-zenhei \ tzdata \ && rm -rf /var/lib/apt/lists/* # 设置时区为中国上海 ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 复制依赖清单并安装 Python 库 COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ && \ pip install gunicorn -i https://mirrors.aliyun.com/pypi/simple/ # 复制整个项目到容器内 COPY . . # 暴露 Flask 服务的 5050 端口 EXPOSE 5050 # 启动脚本: # 1. 清理可能因异常重启遗留的虚拟屏幕锁文件(防止 xvfb 报错退出) # 2. 切换到 web_ui 目录执行 gunicorn # 3. 使用 xvfb-run -a 自动分配空闲的虚拟屏幕 # 4. 浏览器自动化服务必须单 worker 运行,避免多个 Gunicorn 进程同时抢占 Chromium DevTools 端口 # 5. 使用 gthread 提升单进程下的并发响应能力 CMD sh -c "rm -f /tmp/.X*-lock && cd web_ui && xvfb-run -a --server-args='-screen 0 1920x1080x24' gunicorn -w 1 --threads 8 --worker-class gthread -b 0.0.0.0:5050 --access-logfile - --timeout 120 app:app"