What is the difference between volumes and bind mounts?
· Category: Docker
Short answer
Volumes are managed by Docker and stored in a dedicated host directory, while bind mounts map a specific host path into the container. Volumes are preferred for portability and backup, while bind mounts are useful for development.
Key differences
- Volumes: Managed with
docker volume, stored in Docker's data directory, and can be named or anonymous. - Bind mounts: Map any host directory or file directly into the container.
- tmpfs mounts: Store data in host memory only, never written to disk.
Example
Volume:
docker run -v mydata:/data ubuntu
Bind mount:
docker run -v $(pwd)/src:/app/src ubuntu
When to use each
- Use volumes for databases, shared application data, and production workloads.
- Use bind mounts for live code reloading during development.
- Use tmpfs for sensitive data that should not persist.
Common issues
- Bind mounts can cause permission mismatches between host and container users.
- Anonymous volumes are harder to track and can leak disk space.
- SELinux can block bind mounts unless labeled correctly with
:zor:Z.