Optimizing Dockerfile caching for pip

In order for the Docker build cache to cache packages installed via pip, it’s necessary to add the requirements.txt file to the image, and run pip install, before adding the rest of the repo (via ADD .).

Here’s an example of how that might look in a Dockerfile:

FROM python

# If needed, install system dependencies here

# Add requirements.txt before rest of repo for caching
ADD requirements.txt /app/
RUN pip install -r requirements.txt

ADD . /app