🐳 Docker Advanced

What is overlay2 storage driver and how does it work?

Answer

overlay2 is Docker's default and recommended storage driver on Linux (for kernel 4.0+). It implements Docker's layered filesystem using the Linux OverlayFS (overlay filesystem) kernel feature. How it works: OverlayFS merges multiple directories (layers) into a unified view. It has three components: (1) lowerdir — read-only image layers (multiple layers stacked); (2) upperdir — writable container layer; (3) merged — the unified view the container sees. When a container reads a file: OverlayFS looks in upperdir first, then lowerdir. When a container writes/modifies a file that exists only in a read-only lowerdir layer, OverlayFS performs a copy-on-write (CoW): copies the entire file to upperdir, then modifies it there. When a container deletes a lower layer file, OverlayFS creates a "whiteout" file in upperdir to mask it. Storage on disk: /var/lib/docker/overlay2/ — each layer has its own directory. Image layers are shared on disk — multiple containers using the same image share the same lower layers; only the upperdir (container layer) is unique per container. Other storage drivers: aufs (legacy), btrfs, zfs, devicemapper (legacy). Overlay2 is recommended for most production systems.