Mastering Docker Storage: When to Use Storage Drivers vs Data Volumes
This article explains Docker's two data storage mechanisms—storage drivers and data volumes—detailing their architectures, use‑cases, commands, and lifecycle management so you can choose the right approach for stateless or stateful container applications.
Overview
Docker provides two ways to store data: the layers managed by a storage driver and Data Volumes.
1.1 Storage Driver
Containers consist of a writable top layer and several read‑only image layers. New data is written to the top layer, and modifications trigger a copy‑on‑write from the image layer to the container layer, preserving the original image. The storage driver merges these layers into a single view. Docker supports several drivers, including AUFS, Device Mapper, Btrfs, OverlayFS, VFS and ZFS. Because no driver fits every scenario and drivers evolve quickly, Docker recommends using the default driver supplied by the Linux distribution.
Run docker info to see the default driver on Ubuntu.
1.2 Data Volume
A Data Volume is a directory or file on the Docker host that is mounted into a container. Its characteristics are:
It is a directory or file, not an unformatted block device.
Containers can read and write the volume.
The data persists even after the container is removed.
Stateless containers (e.g., busybox) can store data in the image layers, while stateful containers that need persistent data should use a Data Volume.
1.2.1 Bind Mount
Bind mount maps an existing host directory or file into a container, e.g.:
docker run -d -p 4555:80 -v ~/htdocs:/usr/local/apache2/htdocs httpdThe syntax is <host path>:<container path>. You can set the mount as read‑only with :ro:
docker run -d -p 4555:80 -v ~/htdocs:/usr/local/apache2/htdocs:ro httpdBind mounts can also target a single file:
docker run -d -p 4555:80 -v ~/htdocs/index.html:/usr/local/apache2/htdocs/new_index.html httpdAdvantages: immediate host‑container data sharing and easy updates. Drawbacks: requires a specific host path, reducing container portability.
1.2.2 Docker Managed Volume
Docker managed volumes are created with -v without specifying a host source. Docker creates a directory under /var/lib/docker/volumes (or the path set by data-root) and mounts it to the container path. If the mount point already contains data, Docker copies it into the new volume.
Example:
docker run -d -p 4555:80 -v /usr/local/apache2/htdocs httpdYou can inspect the volume location with docker inspect.
Comparison
Aspect
Bind Mount
Docker Managed Volume
Volume location
Any host path
/var/lib/docker/volumes/…
Effect on existing mount point
Hides and replaces with volume
Copies existing data into volume
Supports single file
Yes
No (directory only)
Permission control
Read‑write by default, can set read‑only
Always read‑write
Portability
Weak, tied to host path
Strong, no host path needed
2. Data Sharing
2.1 Container ↔ Host
Both bind mounts and managed volumes enable sharing data between container and host. Bind mounts directly expose a host directory; managed volumes require copying data into the volume before sharing.
You can also copy files with docker cp:
docker cp ~/index.html <container_id>:/usr/local/apache2/htdocs/2.2 Container ↔ Container
Multiple containers can share the same bind mount:
docker run --name web1 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpd
docker run --name web2 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpd
docker run --name web3 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpdOr share a Docker managed volume via a “volume container”:
docker create --name vc_data -v ~/htdocs:/usr/local/apache2/htdocs -v /other/tools busyboxOther containers can use --volumes-from vc_data to inherit those volumes.
2.3 Data‑Packed Volume Container
A Dockerfile can embed data and declare a VOLUME so the image creates a managed volume automatically:
FROM busybox:latest
ADD htdocs /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocsBuild and create the container:
docker build -t datapacked .
docker create --name vc_data_packed datapacked
docker run --name datapacked1 -d -p 80 --volumes-from vc_data_packed httpdThis approach packages the data inside the image, making the container self‑contained and highly portable.
3. Data Volume Lifecycle
3.1 Backup
Since a volume is a directory on the host (typically under /var/lib/docker/volumes), backing it up is equivalent to backing up that part of the host filesystem.
3.2 Migration
To migrate a service, stop the old container and start a new one mounting the existing volume:
docker stop old_container
docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:latest3.3 Destruction
Remove unneeded volumes with docker volume rm. Deleting a container with -v also removes its managed volumes, provided no other container is using them. Orphaned volumes can be cleaned up with docker volume rm $(docker volume ls -q).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
