Day 17 - Docker Volumes and Bind Mounts
1️⃣ The Problem — Why Persistent Storage Is Needed
Containers are ephemeral (temporary).
When a container stops or crashes, all data inside it is lost.
🧩 Example 1: NGINX Logs Lost After Container Stops
Suppose you run an NGINX container that stores user login info and IP addresses in a log file.
These logs are critical for security audits and user tracking.
If the container goes down → the log file is deleted because the container filesystem is temporary.
Result: Company loses important user and audit data.
🧩 Example 2: Frontend–Backend Data Sharing Problem
A backend container continuously writes data (e.g., JSON, YAML, or HTML files).
A frontend container reads these files to display content to users.
If the backend container goes down:
All previously written files are lost.
The frontend can’t access old records (e.g., yesterday’s data).
Result: Broken application — only today’s data is available.
🧩 Example 3: Container Needs to Read Host Files
A cron job on the host creates files periodically.
The container needs to read those files.
By default, a container cannot access the host filesystem.
Result: Container fails to read required host files.
2️⃣ The Solution — Persistent Storage Options
Docker introduced two methods to solve these problems:
Bind Mounts
Volumes
Both allow data to persist even if containers are deleted or recreated.
3️⃣ 🔗 Bind Mounts
🧠 Concept
Bind mounts connect (bind) a folder inside the container to a folder on the host machine.
Example:
Host folder: /app Container folder: /appAny changes made inside the container’s
/appfolder reflect on the host, and vice versa.
⚙️ How It Works
Data is stored on the host.
If the container stops or is removed → the host folder still contains all files.
When a new container is started and the same folder is bound again, the data is retained.
✅ Advantages
Very simple setup.
Great for development environments where you want to see changes instantly.
⚠️ Limitations
Must specify exact host directory path.
Works only on that specific host.
No built-in management via Docker CLI.
4️⃣ 📦 Docker Volumes
🧠 Concept
Volumes provide Docker-managed storage that is independent of the container lifecycle.
You create a volume using Docker commands.
Docker internally manages where and how it’s stored on the host.
🧩 Example
docker volume create mydata
docker run -d --mount source=mydata,target=/app nginx
Now /app inside the container is linked to the Docker volume mydata.
🧰 Lifecycle Management
Create, inspect, delete volumes easily:
docker volume create <name> docker volume ls docker volume inspect <name> docker volume rm <name>Volumes can be attached to one or multiple containers.
⚙️ Features and Benefits
Managed via Docker CLI (no manual path setup).
Logical partitions created on the host.
Can be moved, backed up, and shared across containers.
Can use external storage like:
AWS S3
NFS
External EC2 instance disks
Supports high-performance storage (e.g., SSD/NVMe) for data-intensive apps.
Excellent for production environments.
🧱 Difference from Bind Mounts
| Feature | Bind Mounts | Volumes |
| Storage Location | Host-specified folder | Docker-managed folder |
| Portability | Tied to one host | Can be moved or external |
| Management | Manual | Via Docker CLI |
| Backup Support | Manual | Easy (can connect to remote storage) |
| Use Case | Local dev/test | Production workloads |
5️⃣ 🧩 Volume Command Examples
# Create a new volume
docker volume create myvol
# List all volumes
docker volume ls
# Inspect details
docker volume inspect myvol
# Delete a volume
docker volume rm myvol
6️⃣ 🧠 Practical Example
Step 1 — Create a Volume
docker volume create myvol
Step 2 — Run Container Using Volume
docker run -d --mount source=myvol,target=/app nginx
The container
nginxnow uses/applinked tomyvol.Any file written in
/appis persisted.
Step 3 — Inspect Container Mount
docker inspect <container_id>
You’ll see:
"Mounts": [
{
"Type": "volume",
"Name": "myvol",
"Source": "/var/lib/docker/volumes/myvol/_data",
"Destination": "/app",
"Mode": "rw"
}
]
Step 4 — Delete the Volume
You cannot delete a volume that’s in use:
docker volume rm myvol
# Error: volume is in use
So first stop and remove the container:
docker stop <container_id>
docker rm <container_id>
docker volume rm myvol
7️⃣ 🔍 -v vs --mount Option
| Option | Meaning | Notes |
-v | Short syntax | Compact, older style |
--mount | Long syntax | More verbose, easier to read & understand |
Example:
# Short form
docker run -d -v myvol:/app nginx
# Verbose form
docker run -d --mount source=myvol,target=/app nginx
✅ Recommended: Use --mount for clarity in production or team projects.
🏁 Summary
| Concept | Description |
| Bind Mounts | Directly link container path ↔ host path. Simple but less flexible. |
| Volumes | Docker-managed storage. Persistent, portable, and powerful. |
| Use Volumes When | You need container data persistence, backup, or sharing between multiple containers. |
| Key Commands | docker volume create, docker volume ls, docker volume inspect, docker volume rm |
| Best Practice | Prefer Volumes over Bind Mounts for production-grade applications. |