# 使用官方 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 # 这里替换源以加速下载,并安装必要的系统依赖 # Linux 生产环境优先使用 Google Chrome Stable,避免 Chromium 在 # DevTools WebSocket 握手阶段与 DrissionPage 出现兼容性问题。 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 \ ca-certificates \ curl \ gnupg \ fonts-wqy-zenhei \ tzdata \ && install -m 0755 -d /etc/apt/keyrings && \ curl -fsSL https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /etc/apt/keyrings/google-chrome.gpg && \ echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \ apt-get update && \ apt-get install -y --no-install-recommends google-chrome-stable && \ rm -rf /var/lib/apt/lists/* # 设置时区为中国上海 ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone ENV CHROME_BIN=/usr/bin/google-chrome # 复制依赖清单并安装 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. 切换到 web_ui 目录执行 gunicorn # 2. 浏览器自动化服务必须单 worker 运行,避免多个 Gunicorn 进程同时抢占 DevTools 会话 # 3. Headless Chrome 已足够,无需再叠加 Xvfb,减少 Linux 初始化链路的不确定性 # 4. 使用 gthread 提升单进程下的并发响应能力 CMD sh -c "cd web_ui && gunicorn -w 1 --threads 8 --worker-class gthread -b 0.0.0.0:5050 --access-logfile - --timeout 120 app:app"