Docker build cache invalidation: when a layer stops being reused
Answer: The builder starts by checking whether the base image is already cached, then compares each later instruction against cached layers.
When a layer is reused
In most cases, comparing the Dockerfile instruction with the corresponding cached layer is sufficient. Some instructions require additional checks.
If no cached layer matches the instruction exactly, the cache is invalidated.
When the cache checksum is calculated, a file's modification time (mtime) is not taken into account.
If only the mtime of the copied files have changed, the cache is not invalidated.
Once the cache is invalidated, subsequent Dockerfile commands generate new images and the cache isn't used.
To keep the build cache reusable, order the instructions from less frequently changed to more frequently changed where possible.
RUN, secrets, and timestamps
The cache for RUN instructions isn't invalidated automatically between builds.
To force a re-execution of the RUN instruction, change a layer before it, clear the build cache with docker builder prune, or use --no-cache or --no-cache-filter.
Rebuilding the image one week later will still get you the same packages as before.
Changing the value of a secret doesn't result in cache invalidation.
To force cache invalidation after changing a secret value, pass a build argument whose arbitrary value you also change when the secret changes.
WORKDIR respects SOURCE_DATE_EPOCH when it determines cache validity. Changing that build argument between builds invalidates the WORKDIR cache.
For reproducible builds without frequent cache invalidation, use a fixed timestamp.
See also
Claims
- id: C1 claim: A RUN layer is not automatically invalidated on the next build. source: S1 evidence: "The cache for RUN instructions isn't invalidated automatically between builds."
- id: C2 claim: A rebuild a week later can still install the same packages. source: S1 evidence: "Rebuilding the image one week later will still get you the same packages as before."
- id: C3 claim: Changing a secret value does not by itself invalidate the cache. source: S2 evidence: "Changing the value of a secret doesn't result in cache invalidation."
Sources
- [S1] Build cache invalidation. Docker, Inc.. https://docs.docker.com/build/cache/invalidation/ . License: Apache License 2.0. Accessed 2026-11-26.
- [S2] Build cache invalidation (source). Docker, Inc.. https://raw.githubusercontent.com/docker/docs/main/content/manuals/build/cache/invalidation.md . License: Apache License 2.0. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker, Inc., Build cache invalidation; Docker, Inc., Build cache invalidation (source). Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker, Inc., Build cache invalidation; Docker, Inc., Build cache invalidation (source). Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker build cache invalidation: when a layer stops being reused." iinventedlight Library, iil:note:e6rjca76, version of 2026-11-26. https://iinventedlight.com/library/notes/docker-build-cache-invalidation/. License: CC-BY-4.0.
@misc{iil_note_e6rjca76,
author = {{iinventedlight}},
title = {{Docker build cache invalidation: when a layer stops being reused}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/docker-build-cache-invalidation/},
note = {Permanent ID iil:note:e6rjca76. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- docker-build-cache-invalidation.schema.json (schema; CC-BY-4.0)
- docker-build-cache-invalidation.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-build-cache-invalidation.md.