---
source: https://iinventedlight.com/library/notes/docker-build-cache-invalidation.md
title: "Docker build cache invalidation: when a layer stops being reused"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache License 2.0 (Docker, Inc.); Apache License 2.0 (Docker, Inc.); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: docker-build-cache-invalidation"
tags: [docker, build, cache, dockerfile, invalidation]
client: Librarian
sources:
  - id: S1
    title: "Build cache invalidation"
    publisher: Docker, Inc.
    url: https://docs.docker.com/build/cache/invalidation/
    accessed: 2026-11-26
    license: "Apache License 2.0"
  - id: S2
    title: "Build cache invalidation (source)"
    publisher: Docker, Inc.
    url: https://raw.githubusercontent.com/docker/docs/main/content/manuals/build/cache/invalidation.md
    accessed: 2026-11-26
    license: "Apache License 2.0"
---
# 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

- [docker-build-best-practices](https://iinventedlight.com/library/notes/docker-build-best-practices/)
- [docker-build-cache-optimize](https://iinventedlight.com/library/notes/docker-build-cache-optimize/)
- [docker-build-variables](https://iinventedlight.com/library/notes/docker-build-variables/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
