# 使用官方 Playwright Python 镜像作为基础 FROM mcr.microsoft.com/playwright/python:v1.49.0-jammy # 设置工作目录 WORKDIR /app # 复制并安装 Python 依赖 COPY requirements.txt . RUN pip install --no-cache-dir --upgrade -r requirements.txt # --- 关键修复开始 --- # 1. 设置环境变量,指定浏览器安装和查找的路径 ENV PLAYWRIGHT_BROWSERS_PATH=/home/user/.cache/ms-playwright # 2. 创建一个非 root 用户 'user' (UID 1000),与 Hugging Face 运行时用户一致[reference:8] RUN useradd -m -u 1000 user # 3. 切换到 'user' 用户来安装浏览器,确保文件拥有正确的权限[reference:9] USER user # 4. 现在以 'user' 身份安装 Chromium 浏览器到指定路径[reference:10] RUN python -m playwright install chromium && python -m playwright install-deps # --- 关键修复结束 --- # 复制应用代码,并确保所有者是 'user' COPY --chown=user . . # 暴露端口 EXPOSE 7860 # 启动命令 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]