12 Best Practices for Docker Volume Management
12 Docker volume best practices: named volumes, --mount, read-only and tmpfs mounts, labels, backup, restore, safe pruning, permissions, NFS and Compose.
The core Docker volume best practices are: use named volumes for persistent data, keep bind mounts for development, mount read-only wherever possible, use tmpfs for temporary secrets and scratch data, label and name volumes clearly, back up and test restores, prune with care, set ownership deliberately, and use orchestrator storage across hosts.
Containers are disposable; their data often is not. Docker volumes are the mechanism Docker recommends for persisting data created and used by containers. This guide covers how to choose mount types, protect and back up volume data, and avoid the mistakes that cause data loss. It belongs to our Docker series that starts with what Docker is and why it is popular. Commands were checked against the Docker Engine documentation in November 2026.
Key takeaways
- Docker has three mount types: volumes (managed by Docker), bind mounts (a host path) and tmpfs (memory only). Use volumes for production data.
- Prefer
--mountover-vin scripts; it is more explicit and fails if a bind mount source path is missing instead of creating it. - Since Docker Engine 23,
docker volume pruneremoves only anonymous unused volumes by default;--allalso removes unused named volumes. - A volume is not a backup. Copy data out regularly and practise restoring it.
- For data shared across hosts, use a volume driver (for example NFS through the local driver) or move to orchestrator storage such as Kubernetes persistent volumes.
Volumes, bind mounts and tmpfs compared
| Mount type | Where data lives | Managed by | Survives container removal | Best for |
|---|---|---|---|---|
| Named volume | Docker's storage area (on Linux, under /var/lib/docker/volumes) | Docker | Yes | Databases, uploads, any production data |
| Anonymous volume | Same, with a random name | Docker | Yes, unless created with --rm | Short-lived data; avoid for anything important |
| Bind mount | Any host path you choose | You | Yes (it is the host's filesystem) | Local development, config files |
| tmpfs mount | Host memory | Docker | No | Temporary files, sensitive scratch data |
1. Use named volumes for persistent data
Named volumes are created and managed by Docker, are easy to list and back up, and work the same on Linux, Windows and macOS hosts. Anonymous volumes get random IDs that make it hard to tell which data belongs to which service.
docker volume create pgdata
docker run -d --name db \
--mount type=volume,source=pgdata,target=/var/lib/postgresql/data \
-e POSTGRES_PASSWORD_FILE=/run/secrets/pg_password \
postgres:17
Check the official image's documentation for its data directory; some images change it between major versions.
2. Prefer --mount syntax in scripts and automation
The -v and --mount flags both work, but --mount uses named key-value pairs that are easier to read and review. A practical difference: with -v, if a bind mount source path does not exist, Docker creates it as an empty directory; with --mount, Docker reports an error. That error is usually what you want in production.
# Clear and fails fast if /srv/app/config is missing
docker run -d \
--mount type=bind,source=/srv/app/config,target=/etc/app,readonly \
myapp:1.4.2
3. Mount read-only whenever the container does not need to write
Config files, certificates and static content rarely need write access. Mounting them read-only stops a compromised or buggy process from changing them. Use readonly (or ro) with --mount, or :ro with -v.
docker run -d \
--mount type=volume,source=site-content,target=/usr/share/nginx/html,readonly \
nginx:stable
Combine it with a read-only root filesystem (--read-only) and a tmpfs for paths that must be writable, such as /tmp.
4. Use tmpfs for temporary and sensitive scratch data
A tmpfs mount lives in memory and disappears when the container stops. It is useful for temporary files, caches and data you do not want written to disk. tmpfs mounts are available on Linux hosts.
docker run -d --read-only \
--mount type=tmpfs,target=/tmp,tmpfs-size=64m \
myapp:1.4.2
For real secrets, use Docker secrets in Swarm or Compose, or your orchestrator's secret store, rather than environment variables.
5. Name and label volumes so you can manage them
A naming pattern such as project-service-purpose (for example shop-postgres-data) and labels make it easy to find, back up and clean up volumes.
docker volume create \
--label project=shop \
--label backup=daily \
shop-postgres-data
docker volume ls --filter label=backup=daily
Backup scripts can then select volumes by label instead of keeping a hand-written list.
6. Back up volumes and test restores
The Docker documentation shows a simple pattern: run a temporary container that mounts the volume and a host directory, then archive the data.
# Back up
docker run --rm \
--mount type=volume,source=shop-postgres-data,target=/data,readonly \
-v "$(pwd)":/backup \
alpine tar czf /backup/shop-postgres-data.tgz -C /data .
# Restore into a new volume
docker volume create shop-postgres-restore
docker run --rm \
--mount type=volume,source=shop-postgres-restore,target=/data \
-v "$(pwd)":/backup \
alpine tar xzf /backup/shop-postgres-data.tgz -C /data
For databases, file-level copies of a running database can be inconsistent. Use the database's own tool (pg_dump, mysqldump, mongodump) or stop the container briefly. Copy backups off the host, and restore one regularly to prove it works.
7. Prune unused volumes carefully
Unused volumes fill disks over time. docker system df -v shows how much space each volume uses. Since Docker Engine 23 (API 1.42), docker volume prune removes only anonymous volumes that no container uses; add --all (-a) to include unused named volumes too.
docker system df -v
# Remove unused anonymous volumes
docker volume prune
# Remove unused anonymous AND named volumes (destructive)
docker volume prune --all --filter label!=keep
"Unused" only means no container currently references the volume. A stopped database container that was removed leaves its data volume unused, and pruning deletes it permanently. Label volumes you must keep and filter on that label.
8. Set file ownership and permissions deliberately
When an empty named volume is mounted into a container, Docker copies the existing contents of the target directory from the image into the volume, including ownership. Use that to set permissions: create the directory and set its owner in the Dockerfile, then run as a non-root user.
FROM node:22-slim
RUN mkdir -p /app/data && chown node:node /app/data
WORKDIR /app
COPY --chown=node:node . .
USER node
CMD ["node", "server.js"]
Bind mounts keep the host's ownership and numeric user IDs, which is a common cause of "permission denied" in development. Match the container user's UID to the host user, or adjust ownership on the host. On SELinux hosts, add :z (shared) or :Z (private) to bind mounts so Docker relabels the content; never use these on system directories such as /home or /usr.
9. Keep bind mounts for development, not production data
Bind mounts are ideal for mounting source code into a container so changes appear immediately. In production they tie the container to a specific host layout and give the container access to host files. Use named volumes for data and bake application code into the image. Never bind mount sensitive host paths, and treat mounting /var/run/docker.sock as giving the container root-level control of the host.
# compose.yaml for local development
services:
web:
build: .
volumes:
- type: bind
source: ./src
target: /app/src
db:
image: postgres:17
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
Our Docker Compose files explained covers more Compose volume patterns, and Docker Compose in local development explains the workflow.
10. Use Compose named and external volumes intentionally
In Compose, named volumes declared under the top-level volumes key are created with the project name as a prefix. Use external: true for volumes created outside Compose that must never be deleted by docker compose down -v.
services:
db:
image: postgres:17
volumes:
- prod-pgdata:/var/lib/postgresql/data
volumes:
prod-pgdata:
external: true
name: shop-postgres-data
Remember that docker compose down keeps volumes, while docker compose down -v removes named volumes declared in the file (not external ones). Make sure everyone on the team knows the difference.
11. Use volume drivers or orchestrator storage for multi-host data
A local volume lives on one host. If a container moves to another host, its data does not follow. Options:
- The built-in
localdriver can mount NFS or CIFS shares through driver options. - Third-party volume plugins connect to storage systems; check that a plugin is maintained before relying on it.
- In Kubernetes, use PersistentVolumes, PersistentVolumeClaims and CSI drivers instead of Docker volumes.
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=10.0.0.20,rw,nfsvers=4 \
--opt device=:/exports/shop \
shop-shared
Network storage adds latency and has locking behaviour that some databases do not tolerate. Check your database's guidance before putting its data files on NFS.
12. Monitor volume usage and keep data out of images
Watch disk usage on Docker hosts, since a full disk stops every container. Export host filesystem metrics (for example with Prometheus node exporter) and alert well before the disk fills. Keep data out of image layers: anything written into the container's writable layer is lost when the container is removed and makes the container larger. Point every data path the application writes to at a volume or tmpfs. Be cautious with the VOLUME instruction in Dockerfiles, because it creates anonymous volumes on every run if users do not mount their own.
Share data between containers safely
A sidecar pattern, such as a log shipper reading files written by an application, can share a named volume. Mount it read-write in the writer and read-only in the reader, so only one container can change the data.
services:
app:
image: ghcr.io/example/app:2.1.0
volumes:
- applogs:/var/log/app
shipper:
image: fluent/fluent-bit:4.0
volumes:
- applogs:/var/log/app:ro
volumes:
applogs:
Never let two database servers share a data volume; each needs its own.
Quick command reference
| Task | Command |
|---|---|
| Create a volume | docker volume create NAME |
| List volumes | docker volume ls |
| Inspect a volume | docker volume inspect NAME |
| Show disk usage | docker system df -v |
| Remove a volume | docker volume rm NAME |
| Remove unused anonymous volumes | docker volume prune |
| Remove all unused volumes | docker volume prune --all |
| Mount read-only | --mount type=volume,source=NAME,target=/path,readonly |
| Mount tmpfs | --mount type=tmpfs,target=/tmp,tmpfs-size=64m |
Related reading and training
Smaller images and cleaner layers make volumes easier to reason about; see Docker image optimization practices and 12 Dockerfile examples. To test yourself, try the Docker scenario-based interview questions. Our Docker training covers storage, networking and Compose in the classroom in Kharadi, Pune and live online across India.
Frequently asked questions
What is the difference between a Docker volume and a bind mount?
A volume is created and managed by Docker in its own storage area. A bind mount maps any host path into the container and depends on the host's directory layout and permissions. Docker recommends volumes for persistent data.
Where are Docker volumes stored?
On a Linux host using the default local driver, under /var/lib/docker/volumes. On Docker Desktop they live inside the Docker Desktop virtual machine. Use docker volume inspect to see a volume's mountpoint.
Does docker volume prune delete named volumes?
Not by default since Docker Engine 23. It removes unused anonymous volumes only. Adding --all also removes named volumes that no container uses, so filter by label to protect important data.
Do Docker volumes survive container removal?
Yes. Named volumes remain until you delete them. Anonymous volumes created by a container started with --rm are removed with that container.
How do I back up a Docker volume?
Run a temporary container that mounts the volume and a host directory, then archive the volume's contents with tar. For databases, use the database's dump tool for a consistent backup, and test restores regularly.
Can two containers share one volume?
Yes. Mount the same named volume into both. Make sure the application handles concurrent access; two database servers writing to the same data directory will corrupt it.
Why do I get permission denied on a bind mount?
The container process runs as a user ID that does not own the host files. Match the container user's UID to the host owner, adjust ownership on the host, or on SELinux hosts add the :z or :Z option.
Should I use Docker volumes in Kubernetes?
No. Kubernetes has its own storage model. Use PersistentVolumeClaims backed by a CSI driver, or ephemeral volumes such as emptyDir for scratch data.
Official references
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0