Docker volumes and bind mounts both let containers access host filesystem data, but they behave differently. This guide explains when to use each, how to create them, and what to watch for in production.
Volumes vs bind mounts: the core difference
Both volumes and bind mounts make host files or directories available inside a container. The difference is who manages the location and lifecycle.
| Feature | Docker Volume | Bind Mount |
|---|---|---|
| Storage location | Managed by Docker (/var/lib/docker/volumes/) | Any host path you specify |
| Lifecycle | Docker creates and removes with docker volume commands | Exists independently on the host |
| Host path | Abstracted; you do not need to know the path | You specify the exact host path |
| Pre-population | Docker copies container data into new volumes | Host path must already exist |
| Performance | Native speed on Linux | Native speed on Linux; slower on Docker Desktop |
Docker volumes
Volumes are Docker’s preferred mechanism for persistent data. Docker manages the storage location, so you do not need to know or care where files live on the host.
Create and use a volume:
docker volume create db-data
docker run -d -v db-data:/var/lib/postgresql/data --name postgres postgres:15
List and inspect volumes:
docker volume ls
docker volume inspect db-data
Remove a volume when you no longer need it:
docker volume rm db-data
Volumes are ideal for databases, application state, and any data that must survive container restarts and removals.
Bind mounts
Bind mounts map a specific host path into a container. You control the exact location on the host.
docker run -d -v /home/user/app:/app --name web nginx
Bind mounts are useful for development when you want to edit code on the host and see changes immediately inside the container:
docker run -d -v /home/user/code:/usr/share/nginx/html --name dev-web nginx
When to use each
Use volumes for
- Database data directories
- Application state that must persist
- Production workloads where you do not need to know the host path
- Sharing data between containers
Use bind mounts for
- Development with live code reloading
- Configuration files you manage outside Docker
- Host logs or logs you want to access directly
- Certificates or secrets stored at specific host paths
Named volumes vs anonymous volumes
A named volume has a name you create:
docker run -d -v app-data:/data my-image
An anonymous volume has no name and is harder to manage:
docker run -d -v /data my-image
Anonymous volumes persist but are difficult to reference later. Use named volumes for anything important.
Common issues
Permission problems
Files created inside a volume by the container run as root by default. The host user may not be able to read or write them. Fix by matching user IDs or adjusting permissions:
docker run -u 197609:197609 -v /home/user/data:/data my-image
Bind mount path must exist
Unlike volumes, Docker does not create bind mount source paths. If the host directory does not exist, Docker creates it as root-owned, which can cause permission issues.
Volume data is not copied on mount
If you mount a volume over a directory that already has files in the container image, the existing files are hidden by the mount. Docker copies existing data into a new volume only on first creation.
Useful DevStackIO Tools
Quickly test your Docker commands and configurations with our free tools:
- Docker Run to Compose Converter – Convert docker run commands to docker-compose.yml
- JSON Formatter – Format and validate Docker daemon JSON configs
- YAML Formatter – Format and validate docker-compose.yml files
Related Reading
- du and df: Find What is Filling Your Disk on Linux – Debug volume disk usage
- Docker Networking Modes Explained – Container networking with volumes
- Linux File Permissions Reference – Fix volume permission issues
- systemd Service Hardening – Secure Docker systemd services
Frequently Asked Questions
- Can I convert a bind mount to a volume?
- Yes. Stop the container, create a volume (
docker volume create my-data), copy data from the host path to the volume (docker run --rm -v my-data:/vol -v /host/path:/host alpine cp -a /host/. /vol), then restart with the volume. - Are volumes backed up automatically?
- No. Docker does not back up volumes. Use
docker run --rm -v my-volume:/data -v /c/Users/Vinod/Desktop/Project/devstackio:/backup alpine tar czf /backup/backup.tar.gz /dataor a dedicated backup tool. - Can I share a volume between containers on different hosts?
- Not directly. Docker volumes are local to a single host. For multi-host sharing, use a volume plugin (e.g., NFS, Portworx, Rex-Ray) or a distributed filesystem.
- What happens to volume data when I delete a container?
- Volume data persists. Use
docker rm -vto remove anonymous volumes with the container, ordocker volume rmexplicitly for named volumes. - How do I fix permission denied on bind mounts in Docker Desktop?
- On macOS/Windows, ensure the host path is shared in Docker Desktop Settings > Resources > File Sharing. Use
:delegatedor:cachedmount flags for better performance.