|
| 1 | +# First, specify the base Docker image. |
| 2 | +# You can see the Docker images from Apify at https://hub.docker.com/r/apify/. |
| 3 | +# You can also use any other image from Docker Hub. |
| 4 | +FROM apify/actor-python-playwright:3.13 AS builder |
| 5 | + |
| 6 | +# Install necessary build tools (clang, rust, build-essential) and dependencies |
| 7 | +RUN echo "Installing build tools and dependencies..." \ |
| 8 | + && apt-get update && apt-get install -y \ |
| 9 | + build-essential \ |
| 10 | + clang \ |
| 11 | + curl \ |
| 12 | + # Install Rust |
| 13 | + && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \ |
| 14 | + && export PATH="$HOME/.cargo/bin:$PATH" |
| 15 | + |
| 16 | +# Set up environment for Rust |
| 17 | +ENV PATH="/root/.cargo/bin:$PATH" |
| 18 | + |
| 19 | +# Second, copy just requirements.txt into the Actor image, |
| 20 | +# since it should be the only file that affects the dependency install in the next step, |
| 21 | +# in order to speed up the build |
| 22 | +COPY requirements.txt ./ |
| 23 | + |
| 24 | +# Create and activate a virtual environment for Python dependencies in /opt |
| 25 | +RUN python -m venv /opt/.venv |
| 26 | +ENV PATH="/opt/.venv/bin:$PATH" |
| 27 | + |
| 28 | +# Install the packages specified in requirements.txt, |
| 29 | +# Print the installed Python version, pip version |
| 30 | +# and all installed packages with their versions for debugging |
| 31 | +RUN echo "Python version:" \ |
| 32 | + && python --version \ |
| 33 | + && echo "Pip version:" \ |
| 34 | + && pip --version \ |
| 35 | + && echo "Installing dependencies:" \ |
| 36 | + && pip install -r requirements.txt \ |
| 37 | + && echo "All installed Python packages:" \ |
| 38 | + && pip freeze |
| 39 | + |
| 40 | +# Install Playwright and its dependencies |
| 41 | +RUN playwright install-deps && \ |
| 42 | + playwright install |
| 43 | + |
| 44 | +# Use compileall to ensure the runnability of the Actor Python code. |
| 45 | +RUN python3 -m compileall -q . |
| 46 | + |
| 47 | +# Stage 2: Final lightweight runtime image |
| 48 | +FROM python:3.13-slim-bookworm |
| 49 | + |
| 50 | +# Set the PATH to include the virtual environment's bin directory |
| 51 | +ENV PATH="/opt/.venv/bin:$PATH" |
| 52 | + |
| 53 | +# Copy virtual environment with installed dependencies |
| 54 | +COPY --from=builder /opt/.venv /opt/.venv |
| 55 | + |
| 56 | +# Copy the Playwright dependencies from the builder |
| 57 | +COPY --from=builder /root/.cache/ms-playwright/chromium_headless_shell-1155 /root/.cache/ms-playwright/chromium_headless_shell-1155 |
| 58 | + |
| 59 | +# Copy the Playwright dependencies (system libraries) from the builder (directory would be /usr/lib/aarch64-linux-gnu on ARM) |
| 60 | +COPY --from=builder /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu |
| 61 | + |
| 62 | +# Copy application source code |
| 63 | +COPY . ./ |
| 64 | + |
| 65 | +# Run the application |
| 66 | +CMD ["python3", "-m", "src"] |
0 commit comments