Image layer
A container image is built from stacked, immutable layers — where each layer is the difference (diff) a build step added. The layers are cached and shared between images, which makes building and distribution fast and space-efficient.
Mechanic: each instruction in a Dockerfile (RUN, COPY, etc.) normally creates a new layer on top of the previous one; at runtime all layers are stacked via a union filesystem (overlayfs) into a coherent filesystem, with a writable top layer for the container. Consequences for optimization: caching — unchanged layers aren't rebuilt (put rarely-changed things, like dependency installation, early in the Dockerfile and the code last for the best cache hit); sharing — several images built on the same base layer share them on disk and on download (fetch only the layers you're missing). Pitfall: a secret value placed in a layer remains in the history even if a later layer "removes" it → use multi-stage builds. Fundamental to understanding image size and build performance. Related to multi-stage build and overlayfs.