326

I've recently seen the --no-cache-dir being used in a Docker file. I've never seen that flag before and the help is not explaining it:

 --no-cache-dir              Disable the cache.
  1. Question: What is cached?
  2. Question: What is the cache used for?
  3. Question: Why would I want to disable it?
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

6 Answers6

256
  1. Cached is: store away in hiding or for future use
  2. Used for
  • store the installation files(.whl, etc) of the modules that you install through pip
  • store the source files (.tar.gz, etc) to avoid re-download when not expired
  1. Possible Reason you might want to disable cache:
  • you don't have space on your hard drive
  • previously run pip install with unexpected settings
    • eg:
      • previously run export PYCURL_SSL_LIBRARY=nss and pip install pycurl
      • want new run export PYCURL_SSL_LIBRARY=openssl and pip install pycurl --compile --no-cache-dir
  • you want to keep a Docker image as small as possible

Links to documentation

https://pip.pypa.io/en/stable/reference/pip_install/#caching – @emredjan https://pip.pypa.io/en/stable/reference/pip_install/ - @mikea

GG.
  • 21,083
  • 14
  • 84
  • 130
Stack
  • 4,116
  • 3
  • 18
  • 23
167

I think there is a good reason to use --no-cache-dir when you are building Docker images. The cache is usually useless in a Docker image, and you can definitely shrink the image size by disabling the cache.

Philip Tzou
  • 5,926
  • 2
  • 18
  • 27
  • 38
    you can use [`ENV PIP_NO_CACHE_DIR=1`](https://stackoverflow.com/a/60270281/2248627) in docker for python 3.6.10 and higher images – Levon Feb 19 '20 at 00:36
  • Fwiw, with pip 18.1 you need to [set a falsy value](https://pip.pypa.io/en/stable/user_guide/#:~:text=falsy%20values) for that env var to work. For anything else I get a `TypeError: expected str, bytes or os.PathLike object, not int` since the value gets parsed into a `posixpath`. – dtk Feb 22 '21 at 11:48
  • 1
    `RUN pip install --no-cache-dir -r requirements.txt` for docker images – Timothy L.J. Stewart Apr 23 '22 at 22:02
  • Is this still important in **multi-stage builds**? – Alex Peters Jun 05 '22 at 12:16
  • @AlexPeters Depends on how you make the multi-stage builds. I found the cleanest way is to install packages in builder image and copy the whole `site-packages/` to final image. This way you don't need to use any extra parameters of `pip` since the cache are not copied. – Philip Tzou Jun 06 '22 at 16:08
15

Another reason to disable the pip cache - if you run pip as a user that does not yet exist, their home directory will be created, but owned by root.

This happens to us when building Amazon AMIs in a chroot - pip is being run as a user that exists on the builder machine, but not in the chroot jail where the AMI is being constructed. This is problematic as that specific user can now not ssh to what was just built as their .ssh directory is not readable by them.

I can't think of any other reason pip would be run as a user that doesn't exist though, so it's very much an edge case.

PacketFiend
  • 151
  • 1
  • 2
9

Reduce your docker image size if you're having python dependencies in your DockerFile, as your private registries/artifactories or your deployment servcies may have size limitation.

Zoe The Paranoid
  • 472
  • 5
  • 11
2

I get permission error for installation of some pip packages if I don't use --no-cache-dir option.

Building wheels for collected packages: pyyaml, bottleneck, nvidia-ml-py3
  WARNING: Building wheel for pyyaml failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/b1'
  WARNING: Building wheel for bottleneck failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/92'
  WARNING: Building wheel for nvidia-ml-py3 failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/7f'

chown /.cache folder didn't help for some reason but with --no-cache-dir it works ok.

Hrvoje
  • 13,566
  • 7
  • 90
  • 104
1

From fastapi official doc

The --no-cache-dir option tells pip to not save the downloaded packages locally, as that is only if pip was going to be run again to install the same packages, but that's not the case when working with containers.

Basically, there is no need to store whatever package cache you're installing locally since it is not required by docker containers.

bfontaine
  • 18,169
  • 13
  • 73
  • 107