Docker Image Layers
Each instruction in a Dockerfile
(like RUN
, COPY
, or ADD
) creates a new layer. These layers are stacked on top of each other to form a complete Docker image. The base layer is the FROM:
image.
Layers are read-only and don't change, the subsequent layers are overlayed on top to create the appearance of a machine.
Layers are cached, making re-building an image faster because layers that don't change can just be copied and not rebuilt. Moving unchanging layers to be first helps because docker invalidates and rebuilts all changed layers and the layers after them. Docker images can also share layers.
If you are pulling from a registry, Docker only transfers layers that are new or modified.
Combine related commands into a single RUN
instruction to reduce the number of layers:
Inspect Layers of an image with docker history
command, which shows each layer, its size, and the instruction that created it.
As always: This is for my own understanding. Please don't assume it is 100% correct.