Where Docker stores data
By default Docker keeps its on-disk runtime data under/var/lib/docker. Inside that directory you commonly find subdirectories that map to different parts of Docker’s storage model:
Volumes are Docker’s mechanism for persisting data outside a container’s writable layer. While
/var/lib/docker is the default location for Docker’s on-disk data, volumes and bind mounts can store data elsewhere on the host.
Layered image architecture
Docker images are built as a stack of immutable layers. Each Dockerfile instruction that modifies the filesystem (likeRUN, COPY, ADD) produces a new read-only layer containing only the changes from that instruction. Layers are shared between images and containers when possible, which reduces disk usage and speeds up builds.
Example Dockerfile (common conventions):
Layer reuse example
If a second application uses the same base image and dependencies but different source code or entrypoint, Docker will reuse identical layers (base and dependency layers) and only create new layers for the differences. Second app Dockerfile (differences highlighted):Image layers vs. container writable layer
Image layers are read-only. When you run a container from an image, Docker creates a thin writable layer on top of those read-only layers. Any changes made at runtime — logs, temporary files, created or modified files — are stored in that writable layer. Run a container from the image:Copy-on-write (CoW) behavior
If a file is present in a read-only image layer and a process inside the container modifies that file, Docker does not alter the original image layer. Instead, Docker copies the file into the container’s writable layer and applies the modification there; this is the copy-on-write mechanism. The original image remains unchanged; changes are isolated to the container.Writable-layer lifecycle
The writable layer is tied to the container’s lifecycle. When the container is removed, the writable layer and any changes in it are removed as well. To persist data beyond a container’s lifecycle (for databases, uploads, and other stateful workloads), use volumes or bind mounts.Persistent storage: volumes and bind mounts
Docker provides two main mechanisms to persist container data:- Volume mounts — Docker-managed volumes stored under
/var/lib/docker/volumes. - Bind mounts — mount an arbitrary host directory into the container.
Summary of mount types
Preferred syntax: —mount
The-v shorthand is older and succinct but less explicit. The newer, recommended --mount syntax uses key=value pairs and makes the mount type and source/target explicit:
--mount when you want clarity and to avoid ambiguity in complex mounts.
Who manages layers and copy-on-write?
A storage driver implements Docker’s layered model, creates the writable layer for each container, and handles copy-on-write operations. Common storage drivers include:
Which driver Docker uses depends on the host OS, kernel, and Docker configuration. Docker usually selects a sensible default, but you can configure the daemon to use a different driver if required. Drivers differ in performance, stability, and features — choose according to your workload and platform.
Further reading
- Official Docker storage docs: https://docs.docker.com/storage/
- Docker CLI reference for
--mount: https://docs.docker.com/engine/reference/run/#mount-types - To inspect the storage driver and other daemon info: run
docker info
Tip: For most modern Linux distributions,
overlay2 is a solid default. Run docker info to see which storage driver your Docker daemon is using and evaluate alternatives only if you need features or behavior not provided by that driver.