Apify and Crawlee Official Forum

Updated 4 weeks ago

Making my Docker image more efficient

From since I added browser to my stack, the builds take 2 and more minutes. I'm trying to make the builds more efficient, but I'm no expert in setting up the image, so I'd appreciate any help. This is what I do right now:
Plain Text
FROM apify/actor-python:3.12
ARG ACTOR_PATH_IN_DOCKER_CONTEXT

RUN rm -rf /usr/src/app/*
WORKDIR /usr/src/app

COPY . ./

RUN echo "Python version:" \
 && python --version \
 && echo "Pip version:" \
 && pip --version \
 && echo "Installing Poetry:" \
 && pip install --no-cache-dir poetry~=1.7.1 \
 && echo "Installing dependencies:" \
 && poetry config cache-dir /tmp/.poetry-cache \
 && poetry config virtualenvs.in-project true \
 && poetry install --only=main --no-interaction --no-ansi \
 && rm -rf /tmp/.poetry-cache \
 && echo "All installed Python packages:" \
 && pip freeze \
 && echo "Installing Playwright dependencies:" \
 && poetry run playwright install chromium --with-deps

RUN python3 -m compileall -q ./jg/plucker

ENV ACTOR_PATH_IN_DOCKER_CONTEXT="${ACTOR_PATH_IN_DOCKER_CONTEXT}"
CMD ["poetry", "run", "plucker", "--debug", "crawl", "--apify"]

Is there a way to make it faster?
o
f
H
3 comments
Hey, you could use apify/actor-python-playwright as a base image, which already has the browser preinstalled...so you could skip the poetry run playwright install chromium --with-deps part.

the image is 3 months old but there are plans to update it more often
Hi @Honza Javorek, I use a new Python package and project manager, written in Rust named uv: https://docs.astral.sh/uv/. Is extremely fast and work very well for me. Here you have how I configure all my docker images in my actors:

Plain Text
# First, specify the base Docker image.
# You can see the Docker images from Apify at https://hub.docker.com/r/apify/.
# You can also use any other image from Docker Hub.
FROM apify/actor-python:3.11

# Second, copy just requirements.txt into the Actor image,
# since it should be the only file that affects the dependency install in the next step,
# in order to speed up the build
COPY requirements.txt ./

# Install the packages specified in requirements.txt,
# Print the installed Python version, pip version
# and all installed packages with their versions for debugging
RUN echo "Python version:" \
 && python --version \
 && echo "Pip version:" \
 && pip --version \
 # uv is an extremely fast Python package and project manager, written in Rust.
 # https://docs.astral.sh/uv/
 && echo "Installing uv:" \
 && pip install uv \
 && echo "Installing dependencies using uv:" \
 && uv pip install --system -r requirements.txt \
 && echo "All installed Python packages:" \
 && uv pip freeze

# Next, copy the remaining files and directories with the source code.
# Since we do this after installing the dependencies, quick build will be really fast
# for most source file changes.
COPY . ./

# Use compileall to ensure the runnability of the Actor Python code.
RUN python3 -m compileall -q .

# Specify how to launch the source code of your Actor.
# By default, the "python3 -m ." command is run
CMD ["python3", "-m", "src"]


Before it took 60 to 80 seconds to build, now it takes 11 to 15 seconds... about x5 faster...
Try it...
Add a reply
Sign up and join the conversation on Discord