Volumes: where container data actually lives
How a named volume differs from a bind mount, where all of it physically lives on disk, and how not to lose the database when recreating a container.
Updated August 23, 2026
A container is disposable. Everything written inside it disappears with it. Data must live outside, and there are exactly two ways: a named volume and a host directory bind mount.
Two ways and when to use each
services:
db:
image: postgres:17
volumes:
- db-data:/var/lib/postgresql/data # named volume
app:
image: nginx:1.27
volumes:
- ./site:/usr/share/nginx/html:ro # directory bind mount
- ./nginx.conf:/etc/nginx/nginx.conf:ro
volumes:
db-data:
A named volume is created and stored by Docker itself, in /var/lib/docker/volumes/<name>/_data. Permissions inside are set automatically by the image. This is how databases and internal service state are kept.
A directory bind mount is a regular path on the machine's disk. This is how you provide configuration files, website static assets, and certificates: they are convenient to edit in an editor and store in a repository.
The :ro flag makes the mount read-only. Use it everywhere the container does not need write access — it is cheap protection.
Where everything is physically stored
docker volume ls
docker volume inspect db-data
docker inspect -f '{{json .Mounts}}' <container> | python3 -m json.tool
sudo du -sh /var/lib/docker/volumes/* | sort -h | tail
The last command is the first thing to run when disk space runs out. More details: disk space.
docker compose down -vanddocker volume prunedelete volumes permanently. A volume with a database looks “unused” while the container is stopped and falls under cleanup. Before cleaning, look at the list:docker volume ls -f dangling=true.
Copying and migration
Copying a named volume with a disposable container:
docker run --rm -v db-data:/from -v $(pwd):/to alpine \
tar czf /to/db-data.tar.gz -C /from .
Restoring:
docker volume create db-data
docker run --rm -v db-data:/to -v $(pwd):/from alpine \
tar xzf /from/db-data.tar.gz -C /to
For databases, this method is suitable only when the container is stopped: a file-level copy of a running database may be inconsistent. It is better to dump using the database's own tools — pg_dump, mysqldump. See what exactly to copy.
Permissions
The most common mistake with bind mounts is a mismatch of owner. Inside the container, the process runs under its own UID, and a file created by the host as root is inaccessible to it.
docker compose exec app id
sudo chown -R 1000:1000 ./data
Many images accept PUID/PGID variables — this is more convenient than changing the directory owner.
Common mistakes
- Data was written inside the container. You recreated the container during an update — the data disappeared. Check:
docker diff <container>will show what changed in its filesystem; large writes there are a warning sign. - You bind-mounted a directory over a non-empty path in the image. The image's contents are hidden, and the service cannot find its files.
- A relative path in a bind mount. It is resolved from the directory containing
compose.yaml, not from the current one. Running from another location will create a new empty directory. - Volumes were forgotten during OS reinstallation. Reinstallation wipes the disk entirely. The copy must be stored not on this machine.