Module 5: Docker Volumes & Storage
In the last module, we explored Docker Networking and learned how containers communicate with each other. Now, let’s focus on data persistence—one of the most important aspects of running real-world applications.
By the end of this module, you’ll understand:
The difference between bind mounts and volumes
How to create and manage Docker volumes
How to persist database data using volumes
How to share data between multiple containers
🔹 1. Difference Between Bind Mounts and Volumes
Docker containers are ephemeral by default—once removed, all their data is gone. To persist data, Docker provides two main mechanisms:
🔸 Bind Mounts
Map a directory from the host machine into the container.
Useful for development (syncing code between host & container).
Example:
docker run -v $(pwd):/app nginx👉 This mounts your current directory into
/appinside the container.
🔸 Volumes
Managed directly by Docker.
Stored in Docker’s internal location (
/var/lib/docker/volumes/).More portable, secure, and recommended for production.
Example:
docker run -v mydata:/app/data nginx
👉 Rule of thumb: Use bind mounts for local dev, and volumes for production.
🔹 2. Creating and Managing Volumes
Create a volume
docker volume create my-volume
List volumes
docker volume ls
Inspect a volume
docker volume inspect my-volume
Remove a volume
docker volume rm my-volume
🔹 3. Persisting Database Data with Volumes
Let’s see why volumes are crucial with databases.
Example: MySQL without a volume
docker run --name db1 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0
- If you remove this container, all data is gone ❌.
Now with a volume:
docker run --name db2 -e MYSQL_ROOT_PASSWORD=root \
-v mysql_data:/var/lib/mysql -d mysql:8.0
mysql_datavolume stores the database files.Even if you remove the container, the data persists ✅.
🔹 4. Sharing Data Between Containers
Volumes can also be shared across multiple containers.
Example:
# Create a volume
docker volume create shared-data
# Start first container writing to volume
docker run -d --name writer -v shared-data:/data alpine sh -c "echo 'Hello from Writer' > /data/file.txt && tail -f /dev/null"
# Start second container reading from volume
docker run -it --name reader -v shared-data:/data alpine cat /data/file.txt
👉 The second container (reader) can access data written by the first container (writer).
This is extremely useful for scenarios like:
Multiple containers accessing logs
Web apps and workers sharing files
Database + admin tools accessing the same data
🚀 Quick Recap
In this module, you learned:
✅ Difference between bind mounts and volumes
✅ How to create and manage volumes
✅ How to persist database data
✅ How to share data between containers
