Skip to content

Commit e57d00d

Browse files
committed
feat: add Dockerfile
Inspired by our Apify templates e.g.: - https://github.com/apify/actor-templates/tree/master/templates/python-playwright - https://github.com/apify/actor-templates/tree/master/templates/python-start Features: - build `minify-html` with Rust - optimize Dockerfile by adding multi-stage builds and virtual environment - to reduce image size
1 parent be3dd82 commit e57d00d

File tree

1 file changed

+66
-0
lines changed
  • advanced_tools_frameworks/web_scrapping_ai_agent/.actor

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)