0

I want to install python packages within docker containers. Since the build of some packages is time-consuming and sometimes fails, I want to use a cache that is reused.

I found the mount type cache in the docker documentation and this question Using a pip cache directory in docker builds

But pip always downloads the package without using the cache. Moreover the cache is always empty when building again.

# syntax = docker/dockerfile:experimental                           
FROM python                       
RUN --mount=type=cache,target=/root/.cache/pip ls -la /root/.cache/pip                                
RUN --mount=type=cache,target=/root/.cache/pip pip cache list       
RUN --mount=type=cache,target=/root/.cache/pip pip install tqdm     
CMD python -c "print('Hello World')"

I build with the following command:

docker build -t test --progress=plain . --no-cache

How can I use the cache? The underlying problem is that the pip packages, I use, need to be compiled from source by pip. I used —prefer-binary, but nevertheless some packages are not available as binary for my architecture (raspberry pi). The binary package building fails and I have to retry with some new dev dependencies that are missing. I want to use the compiled wheels from the cache for the packages that already succeeded.

Dronakuul
  • 147
  • 9
  • 3
    This sounds like a good use case for a base image. Build an image that has those dependencies and only that. Then use `FROM your_base_image` to reuse it – C.Nivs Jul 03 '23 at 18:07

0 Answers0