Usman Arif
Home Blog › Docker Layer Caching: The Speedrun

20 January 2026 · 4 min

Docker Layer Caching: The Speedrun

#docker#performance
TL;DR

Order your Dockerfile from least- to most-frequently-changed. Copy dependency manifests and install before copying source, and every unchanged build reuses the cache.

Slow Docker builds are usually self-inflicted. Docker caches each layer and reuses it until something changes — so if you invalidate the cache on every commit, you pay full price every time. The fix is almost always just ordering.

Order by change frequency

Put the things that rarely change at the top and the things that change constantly at the bottom. Base image and system packages first, then dependencies, then — last of all — your source code, which changes on every commit.

Copy manifests before source

The classic win: copy just your `package.json` (or `requirements.txt`, `pyproject.toml`) and install dependencies before copying the rest of the code. Now a code-only change reuses the entire dependency layer instead of reinstalling the world.

The takeaway

Five lines reordered can turn a multi-minute build into seconds on every unchanged push. Your CI bill and your patience will both notice.

Building something with AI, agents or RAG?

Usman Arif builds production LLM features and full-stack apps for teams worldwide. Let’s talk.

usman.professional01@gmail.com

Next: Shipping AI Agents That Actually Do the Work