Kritim Yantra
Apr 22, 2025
If you're just starting with Docker, you might have heard about volumes and bind mounts. Both help you persist data in Docker containers, but they work differently. In this guide, we'll break them down in simple terms with examples.
By default, Docker containers are ephemeral—meaning any data inside them disappears when the container stops. But what if you want to:
This is where volumes and bind mounts come in!
Volumes are the preferred way to persist data in Docker. They are managed by Docker and stored in a special location on the host machine.
✅ Managed by Docker – You don’t need to worry about file paths.
✅ Better performance – Great for databases (like MySQL, PostgreSQL).
✅ Can be shared across containers – Multiple containers can use the same volume.
✅ Backup & migration-friendly – Easy to export and import.
docker volume create my_volume
This creates a volume named my_volume
stored in Docker’s storage directory (/var/lib/docker/volumes/
on Linux).
docker run -d --name mysql_container -v my_volume:/var/lib/mysql mysql
Here, MySQL stores its data in /var/lib/mysql
, which is mapped to my_volume
.
docker volume ls # List all volumes
docker inspect my_volume # See details
Bind mounts link a directory on your host machine to a directory in the container. Unlike volumes, they are not managed by Docker.
✅ Direct file access – Edit files on your host, and changes reflect instantly in the container.
✅ Useful for development – Great for live code reloading.
❌ Less secure – Containers can modify host system files.
❌ Tied to host’s filesystem – Not portable across different systems.
docker run -d --name nginx_container -v /path/on/host:/usr/share/nginx/html nginx
Here:
/path/on/host
= A folder on your computer./usr/share/nginx/html
= Where Nginx stores web files.Now, if you edit files in /path/on/host
, the changes appear inside the container!
docker inspect nginx_container | grep "Mounts" # Check mounted directories
Feature | Docker Volume | Bind Mount |
---|---|---|
Managed by Docker | ✅ Yes | ❌ No |
Persists after container removal | ✅ Yes | ❌ No (unless host files kept) |
Good for production databases | ✅ Yes | ❌ No |
Good for development | ❌ No | ✅ Yes |
Portable across systems | ✅ Yes | ❌ No |
Performance | ✅ Better | ⚠️ Depends on host FS |
tmpfs
Mounts (Memory-Only Storage)There’s a third option—tmpfs
—which stores data only in RAM (disappears when the container stops). Useful for temporary files.
docker run -d --name tmpfs_container --tmpfs /app/cache nginx
Now you know how to keep your Docker data safe and efficient! 🚀
Got questions? Drop them in the comments! 👇
Happy Dockering! 🐳
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 19, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google