Docker Layer
Read-only filesystem layers produced by each Dockerfile instruction and stacked via OverlayFS, enabling layer caching and disk sharing across containers
Table of Contents
Structure Diagram
What is Docker Layer?
Read-only filesystem layers produced by each Dockerfile instruction and stacked via OverlayFS, enabling layer caching and disk sharing across containers. It is a core concept in Containers & Orchestration within Cloud / DevOps.
Core Principles
A Docker Layer represents the filesystem diff created by a single Dockerfile instruction (FROM, RUN, COPY), stored as an immutable, content-addressable read-only layer. At runtime, OverlayFS merges these layers into a unified view and mounts a thin writable container layer on top using Copy-on-Write (CoW).
Because unchanged lower layers are cached and shared across images and running containers, builds, pushes, and node pulls only transfer the deltas that actually changed.
Practical Takeaways
- Order instructions from least to most frequently changed: Copy dependency manifests (
package-lock.json,go.mod) and install packages before runningCOPY . .so everyday source edits never invalidate the dependency layer cache. - Clean up temporary files in the same
RUNlayer: Deleting a file in a subsequentRUNinstruction only writes a whiteout marker and does not shrink the image; chain download, install, andrm -rfin one command. - Maintain a strict
.dockerignore: Exclude.git,node_modules, and local build artifacts so they never bloat layers or bust caches.
Notable Examples
- Production Web App Dockerfiles: Separating lockfile installation from app source copy cuts CI image rebuilds from minutes to seconds.
- Kubernetes / containerd Node Caching: Shared base OS layers are pulled once per node and reused by dozens of pods.
diveCLI: Inspects layer-by-layer size and wasted bytes during container image optimization.