← All notes

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

Sources

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

Permanent ID: iil:note:x2herkw7

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.},
}

Markdown

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.

All artifact files (JSON index)

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.