Skip to content
Opsfolk

Building images, topic 20 of 41

Layer caching

Each instruction makes a layer, and Docker reuses a layer from cache while nothing it depends on has changed. Once one layer changes, every layer after it is rebuilt.

So order a Dockerfile from what changes least to what changes most: copy the dependency manifest, such as package.json and its lock file, install dependencies, and only then copy the source. A code change then skips the install. BuildKit's cache mounts keep a package manager's cache between builds as well.

On Opsfolk

Nothing of ours for this topic yet: an article is on its way.

Elsewhere

Your progress