1- # 使用 Python 3.10 作为基础镜像
1+ # Dockerfile
2+
3+ ARG PROXY_ADDR="http://host.docker.internal:7890"
24FROM python:3.10-slim-bookworm AS builder
35
4- # 设置环境变量
56ARG DEBIAN_FRONTEND=noninteractive
67ARG PROXY_ADDR
7- ENV HTTP_PROXY=${PROXY_ADDR}
8- ENV HTTPS_PROXY=${PROXY_ADDR}
9- ENV POETRY_HOME="/opt/poetry"
10- ENV POETRY_VERSION=1.8.3
11- ENV PATH="${POETRY_HOME}/bin:${PATH}"
128
13- # 设置代理并安装 curl
14- RUN if getent hosts host.docker.internal >/dev/null 2>&1; then \
15- echo 'Acquire::http::Proxy "http://host.docker.internal:7890";' > /etc/apt/apt.conf.d/99proxy; \
16- elif [ -n "$PROXY_ADDR" ]; then \
9+ RUN if [ -n "$PROXY_ADDR" ]; then \
1710 printf 'Acquire::http::Proxy "%s";\n Acquire::https::Proxy "%s";\n ' "$PROXY_ADDR" "$PROXY_ADDR" > /etc/apt/apt.conf.d/99proxy; \
1811 fi && \
1912 apt-get update && \
20- apt-get install -y --no-install-recommends curl && \
21- apt-get clean && rm -rf /var/lib/apt/lists/* && \
22- rm -f /etc/apt/apt.conf.d/99proxy
13+ apt-get install -y --no-install-recommends curl \
14+ && apt-get clean && rm -rf /var/lib/apt/lists/* && \
15+ if [ -n "$PROXY_ADDR" ]; then rm -f /etc/apt/apt.conf.d/99proxy; fi
2316
24- # 安装 Poetry
17+ ENV HTTP_PROXY=${PROXY_ADDR}
18+ ENV HTTPS_PROXY=${PROXY_ADDR}
19+
20+ ENV POETRY_HOME="/opt/poetry"
21+ ENV POETRY_VERSION=1.8.3
2522RUN curl -sSL https://install.python-poetry.org | python3 - --version ${POETRY_VERSION}
23+ ENV PATH="${POETRY_HOME}/bin:${PATH}"
2624
27- # 设置工作目录
2825WORKDIR /app_builder
2926COPY pyproject.toml poetry.lock ./
3027RUN poetry config virtualenvs.create false --local && \
3128 poetry install --no-root --no-dev --no-interaction --no-ansi
3229
33- # 创建最终的 Python 容器
3430FROM python:3.10-slim-bookworm
3531
36- # 继承环境变量
3732ARG DEBIAN_FRONTEND=noninteractive
3833ARG PROXY_ADDR
34+
3935ENV HTTP_PROXY=${PROXY_ADDR}
4036ENV HTTPS_PROXY=${PROXY_ADDR}
4137
42- # 安装依赖
43- RUN apt-get update && \
38+ # 步骤 1: 安装所有系统依赖。
39+ # Playwright 的依赖也在这里一并安装。
40+ RUN \
41+ if [ -n "$PROXY_ADDR" ]; then \
42+ printf 'Acquire::http::Proxy "%s";\n Acquire::https::Proxy "%s";\n ' "$PROXY_ADDR" "$PROXY_ADDR" > /etc/apt/apt.conf.d/99proxy; \
43+ fi && \
44+ apt-get update && \
4445 apt-get install -y --no-install-recommends \
4546 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdbus-1-3 libdrm2 libgbm1 libgtk-3-0 libnspr4 libnss3 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxdamage1 libxext6 libxfixes3 libxrandr2 libxrender1 libxtst6 ca-certificates fonts-liberation libasound2 libpangocairo-1.0-0 libpango-1.0-0 libu2f-udev \
46- supervisor curl && \
47- apt-get clean && rm -rf /var/lib/apt/lists/*
47+ supervisor curl \
48+ && \
49+ # 清理工作
50+ apt-get clean && \
51+ rm -rf /var/lib/apt/lists/* && \
52+ if [ -n "$PROXY_ADDR" ]; then rm -f /etc/apt/apt.conf.d/99proxy; fi
4853
49- # 创建用户和工作目录
5054RUN groupadd -r appgroup && useradd -r -g appgroup -s /bin/bash -d /app appuser
55+
5156WORKDIR /app
5257
53- # 复制 Python 包
58+ # 步骤 2: 复制 Python 包和可执行文件。
59+ # 这是关键的顺序调整:在使用 playwright 之前先把它复制进来。
5460COPY --from=builder /usr/local/lib/python3.10/site-packages/ /usr/local/lib/python3.10/site-packages/
5561COPY --from=builder /usr/local/bin/ /usr/local/bin/
5662COPY --from=builder /opt/poetry/bin/poetry /usr/local/bin/poetry
5763
5864# 复制应用代码
5965COPY . .
6066
61- # 安装 Playwright
62- RUN camoufox fetch && python -m playwright install firefox
63-
64- # 目录权限设置
65- RUN mkdir -p /app/logs /app/auth_profiles/active /app/auth_profiles/saved /app/certs /app/browser_utils/custom_scripts \
66- /home/appuser/.cache/ms-playwright /home/appuser/.mozilla && \
67- chown -R appuser:appgroup /app /home/appuser
67+ # 步骤 3: 现在 Python 模块已存在,可以安全地运行这些命令。
68+ # 注意:我们不再需要 `playwright install-deps`,因为依赖已在上面的 apt-get 中安装。
69+ RUN camoufox fetch && \
70+ python -m playwright install firefox
71+
72+ # 创建目录和设置权限
73+ RUN mkdir -p /app/logs && \
74+ mkdir -p /app/auth_profiles/active && \
75+ mkdir -p /app/auth_profiles/saved && \
76+ mkdir -p /app/certs && \
77+ mkdir -p /app/browser_utils/custom_scripts && \
78+ mkdir -p /home/appuser/.cache/ms-playwright && \
79+ mkdir -p /home/appuser/.mozilla && \
80+ chown -R appuser:appgroup /app && \
81+ chown -R appuser:appgroup /home/appuser
6882
69- # 复制 supervisord 配置
7083COPY supervisord.conf /etc/supervisor/conf.d/app.conf
7184
72- # camoufox 缓存逻辑
85+ # 修复 camoufox 缓存逻辑
7386RUN mkdir -p /var/cache/camoufox && \
7487 if [ -d /root/.cache/camoufox ]; then cp -a /root/.cache/camoufox/* /var/cache/camoufox/; fi && \
75- mkdir -p /app/.cache && ln -s /var/cache/camoufox /app/.cache/camoufox
88+ mkdir -p /app/.cache && \
89+ ln -s /var/cache/camoufox /app/.cache/camoufox
7690
77- # 更新浏览器数据
7891RUN python update_browserforge_data.py
7992
8093# 清理代理环境变量
8194ENV HTTP_PROXY=""
8295ENV HTTPS_PROXY=""
8396
84- # 设置端口
85- EXPOSE 2048 3120
97+ EXPOSE 2048
98+ EXPOSE 3120
8699
87- # 设置用户和环境变量
88100USER appuser
89101ENV HOME=/app
90102ENV PLAYWRIGHT_BROWSERS_PATH=/home/appuser/.cache/ms-playwright
103+
91104ENV PYTHONUNBUFFERED=1
105+
92106ENV PORT=8000
93107ENV DEFAULT_FASTAPI_PORT=2048
94108ENV DEFAULT_CAMOUFOX_PORT=9222
@@ -99,5 +113,4 @@ ENV AUTO_CONFIRM_LOGIN=true
99113ENV SERVER_PORT=2048
100114ENV INTERNAL_CAMOUFOX_PROXY=""
101115
102- # 启动 supervisord
103116CMD ["/usr/bin/supervisord" , "-c" , "/etc/supervisor/conf.d/app.conf" ]
0 commit comments