8.2.1.3 Volume Database Storage
A focused guide to Volume Database Storage, connecting core concepts with practical Docker and container operations.
Volume database storage is the standard, expected pattern of backing a containerized database's actual data directory with a volume, ensuring the database's stored data survives independently of the specific container instance running the database software at any given time.
The Standard Pattern in Practice
A database's data directory is mounted to a named volume, ensuring its content persists regardless of what happens to the container itself.
docker run -d --name postgres-db -v pgdata:/var/lib/postgresql/data postgres:16
Why Official Database Images Typically Document This Pattern
Official database images commonly document exactly which internal path holds the database's actual data, specifically so users know to back that particular path with a volume.
docker run -d -v mysql-data:/var/lib/mysql mysql:8
docker run -d -v mongo-data:/data/db mongo:7
docker run -d -v redis-data:/data redis:7
Each of these reflects the documented data directory for its respective database image, the path that genuinely needs volume backing for that database's data to actually persist.
Why Skipping This Step Risks Catastrophic Data Loss
A database container run without this volume backing functions normally right up until it's removed or recreated, at which point its entire stored dataset is permanently lost — a consequence severe enough that this pattern should be treated as essentially mandatory for any database container holding data that matters.
docker run -d postgres:16
Without the -v flag specifying a volume for the data directory, this database's data exists only in its container's ephemeral writable layer, at serious risk.
Verifying the Pattern Is Correctly Applied
Confirming a running database container actually has its data directory backed by a volume validates the configuration before relying on it.
docker inspect postgres-db --format '{{json .Mounts}}'
Why Volume Database Storage Matters
This is one of the most important and broadly applicable specific applications of volumes — any containerized database without this pattern correctly applied is one container removal away from a complete, permanent loss of its entire stored dataset.