Quick Read: Improving Build Times with Docker + Python

I recently found myself frustrated by how much time I was wasting waiting on Docker to rebuild my Python image anytime a dependency changed. I knew there had to be a better way.

Quick Read: Improving Build Times with Docker + Python
Photo by Timothy Dykes / Unsplash

I recently found myself frustrated by how much time I was wasting waiting on Docker because I had to rebuild my image anytime I changed a dependency in my project's requirements.txt file. I knew there had to be a better way, and I finally decided to look for it. It turns out the solution is quite simple, and I'm kicking myself for not doing this sooner.

This post covers how to decrease Docker build times for Python images by mounting a cache for dependency installation.

The Solution

Ironically, I found what I was looking for in part of a related StackOverflow question rather than an answer. The solution is to use the --mount argument in conjunction with the RUN instruction used to call pip install. Per the Docker Documentation:

The persistent cache helps speed up build steps, especially steps that involve installing packages using a package manager. Having a persistent cache for packages means that even if you rebuild a layer, you only download new or changed packages.

Cache mounts are created using the --mount flag together with the RUN instruction in the Dockerfile. To use a cache mount, the format for the flag is --mount=type=cache,target=<path>, where <path> is the location of the cache directory that you wish to mount into the container.

Thus, the full Dockerfile instruction could look something like this:

RUN --mount=type=cache,target=/root/.cache/pip pip install -r /path/to/requirements.txt

Wrapping Up

Implementing this simple change to my Dockerfile resulted in a significant decrease in the subsequent build times of my Python Docker images. Honestly, the biggest takeaway from this is experience was that it's often much easier to find a valuable, time-saving fix than one might expect.

via GIPHY

Thanks for reading!