Use it to choose how a container stores persistent data.
Volumes vs Bind Mounts
Volumes are managed by the container engine (stored under its data directory); bind mounts map an existing host path into the container.
1. docker inspect <c> Mounts: type volume or bind?
2. Permissions match the container user?
3. SELinux: :z/:Z on bind mounts.
4. Backups cover the volume path.
5. Avoid binding sensitive host paths.
volume or host path → mounted at container path
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Write a test file inside the container, recreate the container, and confirm the file remains. For bind mounts, confirm the host directory exists before the container starts and that the container UID can write to it.
Step 2 Common failures Narrow the fault
A bind mount over a directory hides the image content at that path. Permission denied often means the host directory is owned by a different UID than the container user. Removing a container with docker rm -v deletes anonymous volumes.
Volumes are portable and safer for most data; bind mounts suit config files and development. Bind mounts expose host paths and need matching ownership and SELinux labels.
docker ps -a— containers and exit codesdocker logs <c>— container outputdocker inspect <c>— config, mounts, network, state
ReferenceWhat it is1 section · depth on demand
How it works
A named volume lives under /var/lib/docker/volumes by default. When an empty volume is mounted over a directory that has content in the image, Docker copies that content into the volume first; bind mounts never do this. Volume drivers can place data on NFS or other storage.
ReferenceImportant terms & boundaries3 sections · depth on demand
Volumes vs bind mounts
| Volume | Bind mount | tmpfs | |
|---|---|---|---|
| Managed by | Docker | Host path | Memory |
| Location | /var/lib/docker/volumes/ | Any host path | RAM |
| Portable | Yes | No | — |
| Use | Databases, persistent app data | Config files, development | Secrets, scratch |
docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:17
docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx
In depth
Decision rule
- Data the application owns (database files, uploads) → named volume.
- Files you edit on the host (config during development, source code) → bind mount.
- Scratch data that must not touch disk (secrets, caches) → tmpfs.
Behaviour that surprises people
| Situation | Named volume | Bind mount |
|---|---|---|
| Mounted over a directory that has content in the image | Empty volume is pre-filled with that content | Host directory hides the image content |
| Host path missing | n/a | -v creates an empty root-owned directory; --mount fails with an error |
Removed with docker rm | No (only with -v or docker volume rm) | Never |
| SELinux hosts | Labelled automatically | Needs :z (shared) or :Z (private) relabel |
Ownership
Files written in a volume or bind mount carry the container process's numeric UID. If the image runs as UID 999 and the host directory is owned by UID 1000, writes fail with permission denied. Fix the ownership on the host, run the container with a matching --user, or use a named volume and let the image initialise it.
Worked backup of a named volume
docker run --rm -v pgdata:/data:ro -v "$PWD":/backup alpine \
tar czf /backup/pgdata-$(date +%F).tgz -C /data .
For a running database, a file copy like this is only consistent if the database is stopped; otherwise use the database's own dump or backup tool.
Common mistakes
- Bind mounts with wrong ownership/SELinux labels.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.