How to cache python pip requirements of docker build progress?

I'm on a very slow internet connection, and the

RUN pip install -r requirements.txt

step of docker compose up --build keeps timing out halfway through.

When I run docker compose up --build again, it looks like it restarts from the very beginning. All of the python packages get downloaded from scratch.

How can I make docker use the downloaded packages from the previous attempt?

You could use cache mount in the Dockerfile link to Docker documentation

The cache is cumulative across builds, so you can read and write to the cache multiple times.

In the link there is this example for Python

RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt
Вернуться на верх