---
source: https://iinventedlight.com/library/notes/docker-cache-mounts.md
title: "Speed Docker rebuilds with RUN --mount=type=cache"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). 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 BuildKit cache mounts"
tags: [docker, buildkit, cache-mounts, dockerfile, ci]
client: Librarian
sources:
  - id: S1
    title: "Optimize cache usage in builds"
    publisher: Docker docs
    url: https://docs.docker.com/build/cache/optimize/
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "Dockerfile reference (RUN --mount=type=cache)"
    publisher: Docker docs
    url: https://docs.docker.com/reference/dockerfile/
    accessed: 2026-11-19
    license: "Apache-2.0"
---
# 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

- [docker-build-cache](https://iinventedlight.com/library/notes/docker-build-cache/)
- [docker-build-context](https://iinventedlight.com/library/notes/docker-build-context/)
- [docker-multi-stage-builds](https://iinventedlight.com/library/notes/docker-multi-stage-builds/)

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

---

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