I'm using --mount=type=cache
in my Dockerfile
to cache pip packages between builds, but it doesn't seem to have any effect on build speed. I expected pip to reuse cached packages and avoid re-downloading them, but every build takes the same amount of time.
Here is the relevant part of my Dockerfile:
FROM python:3.13-slim AS compile-image
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir --upgrade pip
# Final image
FROM python:3.13-slim
ENV PATH="/opt/venv/bin:$PATH"
COPY --from=compile-image /opt/venv /opt/venv
WORKDIR /app
ENV HOME=/app
COPY . .
RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \
pip install -e .
Shouldn't this cache downloaded packages and reuse them in subsequent builds? What could be the reason it's not working as expected?
The pip cache is relative to the user's home directory. Just above the RUN pip install
line you set ENV HOME=/app
, and so the pip cache is in /app/.cache/pip
(you also see that directory in the error message). Use that directory as the cache location
RUN --mount=type=cache,target=/app/.cache/pip pip ...
# ^^^^^ the directory you set as $HOME
For most purposes in a container, though, "home directory" isn't an especially meaningful concept, and there's no reason to change it. If you don't set $HOME
and you are running as root then the default home directory will be /root
and the invocation you had before should work.
# ENV HOME=/home # default value, don't need to set this explicitly
RUN --mount=type=cache,target=/root/.cache/pip pip ...
# ^^^^^^ default value of $HOME for root
You can also use this caching in combination with Docker's normal layer caching. You probably want to run the pip install
command in the first stage if you do have a multi-stage build (the setup you show gains little from it), and if you can do it only copying in the pyproject.toml
file first, then you'll avoid re-running the installation sequence if source files but not application dependencies change.