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
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
RUNinstruction"
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Docker build cache optimization: order layers, mounts, and shared cache." iinventedlight Library, iil:note:a12fp370, version of 2026-11-26. https://iinventedlight.com/library/notes/docker-build-cache-optimize/. License: CC-BY-4.0.
@misc{iil_note_a12fp370,
author = {{iinventedlight}},
title = {{Docker build cache optimization: order layers, mounts, and shared cache}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/docker-build-cache-optimize/},
note = {Permanent ID iil:note:a12fp370. 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-build-cache-optimize.schema.json (schema; CC-BY-4.0)
- docker-build-cache-optimize.table.json (table; 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-build-cache-optimize.md.