Docker Volumes vs Bind Mounts: A Beginner’s Guide
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.
Why Do We Need Volumes and Bind Mounts?
By default, Docker containers are ephemeral—meaning any data inside them disappears when the container stops. But what if you want to:
- Keep database files even after restarting a container?
- Share configuration files between your host machine and the container?
- Avoid bloating the container with large files?
This is where volumes and bind mounts come in!
1. Docker Volumes
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.
Key Features:
✅ 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.
How to Use Volumes?
Step 1: Create a Volume
docker volume create my_volume
This creates a volume named my_volume stored in Docker’s storage directory (/var/lib/docker/volumes/ on Linux).
Step 2: Use the Volume in a Container
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.
Step 3: Verify the Volume
docker volume ls # List all volumes
docker inspect my_volume # See details
When to Use Volumes?
- Databases (MySQL, MongoDB, etc.)
- When you don’t need direct access to files from the host.
2. Bind Mounts
Bind mounts link a directory on your host machine to a directory in the container. Unlike volumes, they are not managed by Docker.
Key Features:
✅ 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.
How to Use Bind Mounts?
Step 1: Run a Container with a Bind Mount
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!
Step 2: Verify the Mount
docker inspect nginx_container | grep "Mounts" # Check mounted directories
When to Use Bind Mounts?
- Development environments (e.g., live-reloading Node.js/React apps).
- When you need to edit config files directly from the host.
Volumes vs Bind Mounts: Quick Comparison
| 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 |
Which One Should You Use?
- Use Volumes for databases, production apps, and when you don’t need direct host access.
- Use Bind Mounts for development, config files, and when you want real-time file syncing.
Bonus: 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
Final Thoughts
- Volumes = Best for persistent, managed storage.
- Bind Mounts = Best for development & quick access.
Now you know how to keep your Docker data safe and efficient! 🚀
Got questions? Drop them in the comments! 👇
Happy Dockering! 🐳
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.