Docker images are built in layers — each Dockerfile instruction creates a layer, and Docker caches layers to speed up rebuilds. Understanding layers and caching is key to writing efficient Dockerfiles that build fast and produce smaller images.
Layers — each instruction adds a layer
Each Dockerfile instruction (FROM, RUN, COPY, etc.) creates a read-only LAYER:
→ layers stack to form the image; layers are CACHED and SHARED between images
→ if a layer is unchanged, Docker REUSES the cached layer (skips rebuilding it)
→ Layer caching makes rebuilds fast — only changed layers (and those AFTER) rebuild.
