Docker Volumes Explained
Containers are temporary by design. Docker volumes help you persist important data even when containers are stopped, removed, recreated, or upgraded.
Quick answer
Named volume
Managed by Docker and usually stored under Docker’s data directory.
Bind mount
Maps a specific host folder or file into a container.
tmpfs mount
Stores temporary data in memory and disappears after container stops.
Why volumes matter
If you run a database container without a volume, important data can be lost when the container is removed. Volumes separate data from the container lifecycle.
Named volume example
docker volume create mysql_data docker run -d --name mysql \ -e MYSQL_ROOT_PASSWORD=mysecret \ -v mysql_data:/var/lib/mysql \ mysql:8
Bind mount example
docker run -d --name web \ -p 8080:80 \ -v $(pwd)/html:/usr/share/nginx/html \ nginx
Bind mounts are useful during development because local file changes can reflect inside the container.
Volume commands
docker volume ls docker volume inspect mysql_data docker volume rm mysql_data docker volume prune
Backup a named volume
docker run --rm \ -v mysql_data:/data \ -v $(pwd):/backup \ alpine tar czf /backup/mysql_data_backup.tar.gz -C /data .
Restore a named volume
docker run --rm \ -v mysql_data:/data \ -v $(pwd):/backup \ alpine sh -c "cd /data && tar xzf /backup/mysql_data_backup.tar.gz"
Common mistakes
- Running databases without volumes.
- Deleting volumes during cleanup without checking data.
- Using bind mounts in production without proper path controls.
- Mounting over important image directories accidentally.
- Assuming container removal keeps all data safe.
Best practices
- Use named volumes for persistent container data.
- Use bind mounts mainly for development or controlled host paths.
- Back up important volumes.
- Document what data each volume stores.
- Be careful with docker volume prune .
Related Melochip guides
- Docker Learning Path
- Docker Compose Beginner Guide
Official references
- Docker volumes documentation
- Docker bind mounts documentation
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Learn Docker registries, image tags, Docker Hub, private registries, login, push, pull, naming rules, CI/CD us...
2 min read · Sep 17, 2026
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
2 min read · Sep 17, 2026
Prepare for Docker interviews with practical questions and real-time scenarios covering images, containers, Do...
2 min read · Sep 17, 2026
Learn Docker security best practices for images, containers, users, secrets, registries, scanning, least privi...
2 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.