---
source: https://iinventedlight.com/library/notes/docker-build-cache-optimize.md
title: "Docker build cache optimization: order layers, mounts, and shared cache"
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., Optimize cache usage in builds; Docker, Inc., Optimize cache usage in builds (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-optimize"
tags: [docker, build, cache, bind-mount, dockerfile]
client: Librarian
sources:
  - id: S1
    title: "Optimize cache usage in builds"
    publisher: Docker, Inc.
    url: https://docs.docker.com/build/cache/optimize/
    accessed: 2026-11-26
    license: "Apache License 2.0"
  - id: S2
    title: "Optimize cache usage in builds (source)"
    publisher: Docker, Inc.
    url: https://raw.githubusercontent.com/docker/docs/main/content/manuals/build/cache/optimize.md
    accessed: 2026-11-26
    license: "Apache License 2.0"
---
# Docker build cache optimization: order layers, mounts, and shared cache
**Answer:** These techniques can optimize build caching and speed up the build process.

## Order the layers

Putting Dockerfile commands into a logical order can help you avoid unnecessary cache invalidation.

A common snippet copies the current directory first, then runs a JavaScript build from those source files.

The COPY command can be split in two. First copy the package management files, such as package.json and yarn.lock. Then install the dependencies. Finally copy the project source code.

Install dependencies in earlier Dockerfile layers so those layers need not rebuild when a project file has changed.

A .dockerignore file can exclude the node_modules directory and every file or directory that starts with tmp.

## Bind mounts and shared cache

A bind mount puts a host file or directory into the build container, which can avoid unnecessary image layers.

A bind mount is read-only by default. Writing to the mounted directory requires the rw option.

The rw option still does not persist those changes in the final image or the build cache.

File writes last for the duration of the RUN instruction, and are discarded after the instruction is done.

If the target directory is not empty, its contents are hidden by the mounted files, then restored after the RUN instruction is done.

An external cache lets you store build cache at a remote location. That cache image can be shared between multiple builds and across different environments.

## See also

- [docker-build-cache-invalidation](https://iinventedlight.com/library/notes/docker-build-cache-invalidation/)
- [docker-base-images](https://iinventedlight.com/library/notes/docker-base-images/)
- [docker-build-best-practices](https://iinventedlight.com/library/notes/docker-build-best-practices/)

## Claims

- id: C1
  claim: Bind mounts default to read-only during a build.
  source: S1
  evidence: "Bind mounts are read-only by default."

- id: C2
  claim: An external cache can hold build cache in a remote location.
  source: S1
  evidence: "An external cache lets you store build cache at a remote location."

- id: C3
  claim: Writes through a mount last only for that RUN instruction.
  source: S2
  evidence: "The file writes are sustained for the duration of the `RUN` instruction"

## Sources

- [S1] Optimize cache usage in builds. Docker, Inc.. https://docs.docker.com/build/cache/optimize/ . License: Apache License 2.0. Accessed 2026-11-26.

- [S2] Optimize cache usage in builds (source). Docker, Inc.. https://raw.githubusercontent.com/docker/docs/main/content/manuals/build/cache/optimize.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., Optimize cache usage in builds; Docker, Inc., Optimize cache usage in builds (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).
