Image Layer

An image layer is one immutable filesystem diff that, when stacked with the layers above and below it, forms a complete container image. Each Dockerfile instruction (FROM, COPY, RUN, ADD) typically produces one layer. Layers are content-addressable: identical layers are stored once and shared across images.

How layers compose

An image is described by a manifest: an ordered list of layer digests plus a config (entrypoint, env vars, labels). A container is started by stacking layers via a union filesystem (overlayfs is the default on Linux) and overlaying a writable scratch layer on top. The container sees a single merged filesystem; writes go to the scratch layer and disappear when the container is removed.

Why layering matters

  • Caching. A build reuses unchanged layers; only changed layers and their descendants are rebuilt.
  • Pull and push efficiency. Registries store and transfer each unique layer once, regardless of how many images include it.
  • Determinism. The content-addressable digest of a layer is a strong identity. Two layers with the same digest are byte-identical.

Optimisation tips reflected in real Dockerfiles

  • Order instructions from most-stable to most-volatile so caches hit longer.
  • Use --mount=type=cache for package manager caches instead of layer copies.
  • Use multi-stage builds: build in a heavy stage, copy only artifacts to a slim final stage.
🔗

Subscribe to Sahil's Playbook

Clear thinking on product, engineering, and building at scale. No noise. One email when there's something worth sharing.
[email protected]
Subscribe
Mastodon