Two pieces of state live outside a container’s image: things you want Docker to manage (a database’s data directory, a queue’s persistent log) and things the host needs to manage (a TLS certificate, a config file, the socket of another service). Docker offers volumes for the first and bind mounts for the second. Mixing them up is one of the more common production missteps — the right pick depends on who owns the bytes and where they need to live.
How to verify
List what’s mounted into a running container and where it lives on the host:
docker inspect <container> --format '{{json .Mounts}}' | jq
docker volume ls
docker volume inspect <volume-name>
ls -la /var/lib/docker/volumes/<volume>/_data
If the Type field is bind, the source is a path on the host filesystem. If Type is volume, the source is a Docker-managed volume (by default under /var/lib/docker/volumes/). The third type, tmpfs, is RAM-backed and doesn’t persist at all.
Decision
| Need | Use |
|---|---|
| Database data directory | Named volume |
| App configuration file from the host | Bind mount (ro) |
| TLS certificate from the host | Bind mount (ro) |
| Cache directory the app writes to | tmpfs |
| Secret material | tmpfs or Docker secret |
| Files the host orchestrator generates | Bind mount |
| Sharing data between containers | Named volume |
| Backups of stateful data | Named volume |
| Sharing the Docker socket | Bind mount (and think hard) |
Pick a named volume when Docker owns the lifecycle. Pick a bind mount when something on the host owns the lifecycle. Pick tmpfs when the data should never touch disk.
What’s happening
A named volume is a directory Docker creates and manages, by default under /var/lib/docker/volumes/<name>/_data. It has a stable name, can be inspected and backed up with docker commands, supports volume drivers (NFS, CIFS, cloud block storage), and survives container removal until you explicitly docker volume rm it. The container sees the data at whatever mount point you specify; the host path is an implementation detail.
A bind mount maps an exact path on the host into the container. The host owns the directory; Docker just makes it visible inside the container. There’s no Docker-managed lifecycle — if you delete the host path, the bind mount breaks. Permissions follow the host’s UID/GID, which can clash badly with the container’s user namespace.
A tmpfs mount is a RAM-backed filesystem visible only to the container, gone when the container stops. Useful for /tmp, ephemeral caches, and any data you absolutely don’t want on disk (e.g. secrets that get fetched fresh at startup).
The procedure
For a stateful service like PostgreSQL, use a named volume:
docker volume create pg-data
docker run -d --name pg \
--mount type=volume,source=pg-data,target=/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=$(cat /run/secrets/pg_password) \
postgres:16
The --mount long form is more readable than -v. To back up the volume:
# Snapshot the volume contents to a tar file on the host
docker run --rm \
-v pg-data:/source:ro \
-v $(pwd)/backups:/dest \
alpine tar czf /dest/pg-data-$(date +%Y%m%d).tar.gz -C /source .
# Restore into a fresh volume
docker volume create pg-data-restored
docker run --rm \
-v pg-data-restored:/dest \
-v $(pwd)/backups:/source:ro \
alpine tar xzf /source/pg-data-20260602.tar.gz -C /dest
For a configuration file from the host, use a read-only bind mount:
docker run -d --name nginx \
--mount type=bind,source=/etc/nginx/sites-enabled,target=/etc/nginx/conf.d,readonly \
--mount type=bind,source=/etc/letsencrypt,target=/etc/letsencrypt,readonly \
-p 443:443 \
nginx:1.27
readonly is the default position for any bind mount the container shouldn’t write to — it prevents accidental writes from inside the container modifying host files.
For a cache or secrets directory, use tmpfs:
docker run -d --name app \
--tmpfs /tmp:rw,size=128m,mode=1777 \
--tmpfs /var/cache/app:rw,size=64m \
myapp:latest
# Or with --mount:
docker run -d --name app \
--mount type=tmpfs,destination=/tmp,tmpfs-size=134217728 \
myapp:latest
For sharing data between containers on the same host, named volumes shine:
docker volume create shared-uploads
docker run -d --name web \
--mount type=volume,source=shared-uploads,target=/var/www/uploads \
myweb:latest
docker run -d --name worker \
--mount type=volume,source=shared-uploads,target=/data/uploads \
myworker:latest
Both containers see the same data, with Docker handling the storage layer.
For backups of volumes you actually run in production, use the --volumes-from pattern with a backup container, or — more commonly in real ops — script docker exec of the application’s own backup command (pg_dump, mongodump, redis-cli BGSAVE) writing into a dedicated backup volume that gets shipped offsite by a separate process.
Common pitfalls
- Bind-mounting a path that doesn’t exist on the host — Docker silently creates it (as root-owned), and now your app has the wrong owner and may behave unpredictably.
- Bind-mounting host config files writable when the container only needs to read them, and watching the container alter your host config (e.g.
wp-config.phpgetting database creds rewritten by WP). - Using a named volume for a config file that ships in CI — every host then has a stale volume from the first deploy, defeating the immutable image story. Bind-mount config files; use volumes for data only.
- Forgetting that named volumes survive
docker rm— a stale volume can have data from a previous test run, causing confusing fresh-install behavior. - Mounting
/var/run/docker.sock— root on the host. If you must (Watchtower, Portainer), pair with user namespaces and a read-only socket proxy.
In the engagements we run, named volumes get the same backup discipline as any other production data store — included in the managed operations backup schedule, restored quarterly as part of the DR drill.