Card 01/ 07

ConceptDifficulty: Intermediate1 min

Every Instruction Leaves a Layer

A build that took twelve seconds the first time takes under one the second time, and nothing about your machine got faster.

Each instruction in a Dockerfile produces a layer: the filesystem difference it made, stored on its own. The builder keeps those layers, and before running an instruction it asks whether it has already run this exact instruction on top of this exact layer. If it has, it reuses the result instead of doing the work.

Two things follow, and the rest of this topic is both of them. A layer is the unit of caching, so what invalidates a layer decides how long your builds take. A layer is also the unit of storage, so layers are what fills the disk.