Files
crawle-snapp/Dockerfile
T
nfel 1ce57c3ea2 Fix worker rq not found — build venv natively in Playwright image
The Playwright Noble image ships Python 3.12 while the builder uses
python:3.11-slim. Copying the venv produced broken shebangs, causing
"exec /app/.venv/bin/rq: no such file or directory".

Fix: install uv in the worker stage and run uv sync natively so the
venv targets this image's Python version. Manifests copied first for
layer cache efficiency.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-08-04 21:39:51 +03:30

87 lines
3.1 KiB
Docker

# Usage:
# make build # admin + worker — uses python:3.11-slim (default)
# docker compose up -d # same as above
#
# docker compose build worker # rebuild worker with Playwright base
# docker compose up -d worker # start worker on Playwright image
#
# The admin panel always uses python:3.11-slim (no Chrome needed).
# The worker uses mcr.microsoft.com/playwright:v1.62.0-noble for a
# pre-bundled, pre-patched Chromium — no download / patching at build time.
# ── Build args ─────────────────────────────────────────────────────────────────
ARG BASE_IMAGE=python:3.11-slim
# ── Stage 1: dependency resolver ─────────────────────────────────────────────
FROM python:3.11-slim AS builder
# Keep the build independent of GHCR; dependencies already use official PyPI.
RUN pip install --no-cache-dir uv==0.11.31
WORKDIR /app
# Copy only dependency manifests first (cache layer)
COPY pyproject.toml uv.lock* ./
# Install deps into an isolated prefix so we can copy them cleanly
RUN uv sync --frozen --no-dev --no-install-project
# ── Stage 2a: admin runtime (python:3.11-slim, no Chrome) ────────────────────
FROM ${BASE_IMAGE} AS admin
WORKDIR /app
ENV PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
COPY . .
COPY --from=builder /app/.venv /app/.venv
RUN mkdir -p data logs recordings \
&& useradd -m -u 1001 seed \
&& chown -R seed:seed /app
USER seed
EXPOSE 8000
CMD ["python", "main.py", "admin"]
# ── Stage 2b: worker runtime (Playwright image with Chromium) ────────────────
# The Playwright Noble image ships Python 3.12. Copying a venv built with
# python:3.11-slim would produce broken shebangs — so we build the venv here.
FROM oci.reg.darano.ir//mcr.microsoft.com/playwright:v1.62.0-noble AS worker
ENV PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
# Playwright's Chromium binary
CHROME_BINARY=/usr/bin/chromium \
CHROMEDRIVER_PATH=/usr/bin/chromedriver \
# Always run headless inside Docker
HEADLESS=true
WORKDIR /app
# Copy manifests + lockfile first (stable cache layer)
COPY pyproject.toml uv.lock* ./
# Install deps natively so the venv targets this image's Python
RUN pip install --no-cache-dir --upgrade uv==0.11.31 \
&& uv sync --frozen --no-dev --no-install-project
# Copy app code (busts cache on code changes, not deps)
COPY . .
# Playwright's Chromium is already downloaded and pre-patched with
# undetected-chromedriver-friendly modifications, so no patch_driver run needed.
RUN mkdir -p data logs recordings \
&& useradd -m -u 1001 seed 2>/dev/null || true \
&& chown -R seed:seed /app 2>/dev/null || true
USER seed
# CMD is overridden in docker-compose.yml to use RQ worker
CMD ["rq", "worker", "--url", "redis://redis:6379/0", "batch"]