by blackrobot · 6y ago · 0 comments
COPY requirements.txt ./
RUN pip install -r requirements.txt
If you're building the image on a CI server, docker can't cache that step because the files won't match the cache due to timestamps/permissions/etc... The same is true for other developer's machines.This is a problem if your requirements includes anything that uses C extensions, like mysql/postgresql libs or PIL.
1. Using poetry which keeps a version lock file so all changes are reflected/cached, or
2. Doing a similar thing yourself by committing `pip freeze` and building images from that instead of requirements.txt.
https://docs.docker.com/develop/develop-images/dockerfile_be...
> For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.
If the CI system keeps the source tree the Dockerfile is being built from around rather than removing it all after every build, it caches stuff as normal.