Fixing Docker Build Cache: Slow Builds and Stale Layers
Two failures wear the same face: builds that rebuild everything every time, and builds that hand you a stale image. Both trace back to how layer caching keys work.
Key takeaways
- Slow Docker builds usually mean an early COPY busts the cache for every later layer, so copy lockfiles and install dependencies before copying source.
- Add a tight .dockerignore, BuildKit cache mounts and a registry cache for CI.
- Stale builds come from apt-get update on its own line or remote downloads Docker cannot checksum.
On this page
To fix a slow Docker build, copy your lockfile and install dependencies before COPY . ., add a .dockerignore, use BuildKit cache mounts, and export a registry cache in CI. To fix a stale one, chain apt-get update with install in one RUN, and bust the cache deliberately for remote downloads Docker cannot checksum.
Verified against
Docker Engine 29.9.2 (released 2026-10-20) with BuildKit v0.34.0, Dockerfile syntax 1, against the docs.docker.com build cache, cache optimization and registry cache backend pages. Checked October 2026.
Our CI image build crept from ninety seconds to seven minutes over about a year, and nobody noticed the day it happened because it never happened on one day. A dependency here, a reordered COPY there. Then a separate team burned an afternoon chasing a bug that turned out to be a stale layer serving an old config file that had been "fixed" three commits ago. Same subsystem, two opposite symptoms. Once you see how the cache actually keys layers, both stop being mysteries.
How layer caching works
Every instruction in your Dockerfile produces a layer, and Docker computes a cache key for each one. For most instructions the key is the instruction text itself plus the state of the layers before it. For COPY and ADD it also includes a checksum of the files being copied. If the key matches a layer Docker already has, it reuses it and moves on. The moment one key misses, every instruction after it is invalidated too, because each layer's key depends on the one above it.
That cascade is the whole game. A cache miss on line 4 throws away the work on lines 5 through 20 even if those instructions never changed. So the question is never "is my cache working." It's "which line is busting it, and did I put that line in the worst possible spot."
Problem one: builds that never cache
The classic offender is copying your entire source tree before installing dependencies:
# Dockerfile (before)
FROM node:24
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
CMD ["node", "dist/server.js"]
COPY . . checksums your whole working tree. Change one line in any source file and the key for that layer misses, which invalidates npm install and everything below. You reinstall every dependency on every commit, even though your package.json hasn't moved in weeks.
The fix is to order the Dockerfile so rarely-changing layers come first. Copy the lockfile and install before you copy source:
# Dockerfile (after)
FROM node:24
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]
Now npm ci only reruns when package.json or package-lock.json changes. Edit a route handler and the install layer stays cached; Docker jumps straight to COPY . .. This one reordering is the highest-value change most Dockerfiles need, and it costs nothing.
The same logic applies per language. Python: copy requirements.txt and pip install before source. Go: copy go.mod and go.sum and run go mod download first. Rust with Cargo, Ruby with Bundler, same shape. Dependencies change rarely, source changes constantly, so dependencies go up top.
A tight .dockerignore
Even with good ordering, COPY . . will checksum everything in the build context. If your context includes node_modules, .git, build output, and log files, two things go wrong: the context upload is huge, and unrelated file changes bust the copy layer. A local node_modules timestamp shift can invalidate the copy even when no source changed.
A .dockerignore fixes both:
# .dockerignore
node_modules
.git
dist
*.log
.env
coverage
This trims what gets sent to the daemon and keeps churny local files out of the cache key. If you want the full rundown on what belongs in there, we wrote up .dockerignore separately. Treat it as load-bearing, not housekeeping.
BuildKit cache mounts
Ordering keeps the install layer cached across commits, but the first build after a lockfile change still downloads every package from scratch. BuildKit cache mounts fix that by persisting the package manager's own cache directory across builds, outside the image layers:
# syntax=docker/dockerfile:1
FROM node:24
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci
COPY . .
RUN npm run build
The --mount=type=cache directory survives even when the layer itself is rebuilt. Add one dependency and npm ci reruns, but it pulls the unchanged packages from the warm cache instead of the network. For pip use target=/root/.cache/pip, for Go target=/root/.cache/go-build and /go/pkg/mod. For apt, Docker's docs mount both /var/cache/apt and /var/lib/apt with sharing=locked, because apt needs exclusive access to its files. This is the single biggest win on a cold-ish build, and it needs BuildKit, which is the default builder in current Docker Engine releases.
Registry cache in CI
CI runners start clean, so none of the above helps unless the cache lives somewhere the runner can reach. That's what registry cache is for. You export the build cache to a registry and import it on the next run:
docker buildx build \
--cache-from type=registry,ref=myrepo/app:buildcache \
--cache-to type=registry,ref=myrepo/app:buildcache,mode=max \
-t myrepo/app:latest --push .
mode=max exports cache for every stage, not just the final image, which matters for multi-stage builds. The runner pulls the cache metadata, matches layer keys, and skips work it did last time. Set this up once and your CI build behaves like your warm laptop. The same flags carry over unchanged to multi-arch builds with buildx, where a warm cache matters even more because every platform rebuilds its own layers.
Problem two: caches that serve stale results
The opposite failure. Docker happily reuses a layer whose key still matches but whose real-world dependency changed underneath it. The usual trap is RUN apt-get update on its own line: the key never changes, so Docker serves a months-old package index, then apt-get install pulls stale or missing versions. Always chain them in one instruction so they cache and bust together:
RUN apt-get update && apt-get install -y --no-install-recommends curl
The other stale trap is remote content Docker can't see: a git clone, or a curl of a "latest" tarball. The instruction text is identical run to run, so the key matches and you get last week's download. When you genuinely need a fresh pull, reach for --no-cache on that build, or a cache-busting build arg for the one stage that needs it:
docker build --build-arg CACHEBUST=$(date +%s) -t myrepo/app:latest .
docker build --no-cache-filter fetch -t myrepo/app:latest .
The build arg only busts layers from the first instruction that references ARG CACHEBUST, and --no-cache-filter skips the cache for the named stage only. Reserve --no-cache for debugging and for the rare content Docker can't checksum. Using it as your default just means you've given up on the cache entirely.
Multi-stage caching
Multi-stage builds cache per stage, so a heavy builder stage stays warm while your slim runtime stage rebuilds cheaply:
FROM node:24 AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
FROM node:24-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]
Order the COPY lines in the final stage the same way, most stable first. And if you hit a build error that looks like a cache problem but isn't, our Docker troubleshooting guide covers the ones that masquerade as caching bugs.
Questions people ask
Why does Docker rebuild every layer after one change?
Each layer's cache key includes the state of every layer before it, so one miss invalidates everything below it. The usual culprit is COPY . . near the top: any source edit changes its checksum and forces npm ci or pip install to rerun. Copy only the lockfile, install dependencies, then copy the rest of the source.
How do I force Docker to rebuild without cache?
Run docker build --no-cache . to ignore the cache for every instruction. To refresh a single stage, use --no-cache-filter <stage>, or add an ARG CACHEBUST just above the instruction that downloads remote content and pass a new value with --build-arg. Keep these for content Docker cannot checksum, not as a default.
Why is my CI Docker build never cached?
CI runners usually start with an empty builder, so there is no local cache to hit. Export the cache to a registry with --cache-to type=registry,ref=<repo>:buildcache,mode=max and import it with --cache-from on the next run. mode=max caches intermediate stages too, which matters for multi-stage builds.
The call we'd make
Fix ordering first, because it's free and it fixes the most common case. Add a real .dockerignore the same afternoon. Then wire BuildKit cache mounts for your package manager and registry cache in CI, in that order, since the mounts help everywhere and the registry cache only helps runners. Leave --no-cache in the toolbox for the two or three things Docker genuinely can't see, and chain your apt-get lines so the stale case never bites. Do that and a seven-minute build goes back to ninety seconds without anyone noticing the day it happened.
Sources
Cite this article
Kiril Urbonas (2026). Fixing Docker Build Cache: Slow Builds and Stale Layers. DevOpsNess. Updated 2026-11-15. https://www.devopsness.com/blog/fix-docker-build-cache
[Fixing Docker Build Cache: Slow Builds and Stale Layers](https://www.devopsness.com/blog/fix-docker-build-cache) (Kiril Urbonas, DevOpsNess, updated 2026-11-15)
More from DevOps
Explore more articles in this category
How to Undo the Last Git Commit (Every Case)
You committed too early, wrote a bad message, or included the wrong file. Here's the decision tree for undoing a Git commit safely, by what you actually want to keep.
Talos Linux vs Standard Kubernetes Nodes: What Changes When SSH Is Gone
Talos Linux has no SSH, no shell and no package manager. Every node change goes through one mTLS API. Here is what day-2 work looks like without a login, where Talos beats Ubuntu plus kubeadm, and where it will hurt.
GitOps with Argo CD: Best Practices for 2026
Argo CD 3.x patterns for 2026: repo layout, sync policies you can trust, secrets that stay out of git, and the anti-patterns worth avoiding.
You might have missed
Evergreen posts worth revisiting.