Speed Docker rebuilds with RUN --mount=type=cache
Answer: Cache mounts let you specify a persistent package cache to be used during builds. The mount is not committed to the image layer. Regular cache layers correspond to an exact match of the instruction; if inputs have changed, the layer is invalidated. The cache is cumulative across builds, so even if you rebuild a layer, you only download new or changed packages.
Cache mounts versus bind mounts
Cache mounts persist between builder invocations without invalidating the instruction cache. Use them for better performance only. Another build may overwrite the files, or GC may clean the directory.
Bind mounts let you mount a file or directory from the host into the build container. They help you avoid unnecessary layers. Bind mounts are read-only by default and are not persisted in the final image.
Options
| Option | Description | |---|---| | target (also dst) | Mount path in the build container. | | id | Optional ID to identify separate caches. Defaults to target. | | sharing | shared (default), private, or locked. A locked mount pauses the second writer until the first one releases the mount. |
Apt needs exclusive access, so examples use sharing=locked for /var/cache/apt.
Gotcha
The builder keeps cache mounts in local storage. If each build runs on a new builder, cache mounts start empty. If each build runs on a new builder, cache mounts start empty. External caches are especially useful for CI/CD pipelines, where the builders are often ephemeral. Specify --cache-from and --cache-to.
See also
Claims
- id: C1 claim: A cache mount is a persistent location reused across builds. source: S1 evidence: "Cache mounts are a way to specify a persistent cache location to be used during"
- id: C3 claim: sharing=locked serializes writers on one cache mount. source: S2 evidence: "pauses the second writer until the first one releases the mount"
- id: C4 claim: Cache mounts are not exported with --cache-to. source: S2 evidence: "They are not included in exported build cache"
Sources
- [S1] Optimize cache usage in builds. Docker docs. https://docs.docker.com/build/cache/optimize/ . License: Apache-2.0. Accessed 2026-11-19.
- [S2] Dockerfile reference (RUN --mount=type=cache). Docker docs. https://docs.docker.com/reference/dockerfile/ . License: Apache-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Optimize cache usage in builds; Docker docs, Dockerfile reference (RUN --mount=type=cache). Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Optimize cache usage in builds; Docker docs, Dockerfile reference (RUN --mount=type=cache). Apache-2.0; Apache-2.0.
Cite this
iinventedlight. 2026. "Speed Docker rebuilds with RUN --mount=type=cache." iinventedlight Library, iil:note:x2herkw7, version of 2026-11-19. https://iinventedlight.com/library/notes/docker-cache-mounts/. License: CC-BY-4.0.
@misc{iil_note_x2herkw7,
author = {{iinventedlight}},
title = {{Speed Docker rebuilds with RUN --mount=type=cache}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/docker-cache-mounts/},
note = {Permanent ID iil:note:x2herkw7. 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-cache-mounts.table.json (table; CC-BY-4.0)
- docker-cache-mounts.schema.json (schema; 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-cache-mounts.md.