Day 17 - Docker Volumes and Bind Mounts
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Containers are ephemeral (temporary).
When a container stops or crashes, all data inside it is lost.
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.
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.
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.
Docker introduced two methods to solve these problems:
Bind Mounts
Volumes
Both allow data to persist even if containers are deleted or recreated.
Bind mounts connect (bind) a folder inside the container to a folder on the host machine.
Example:
Host folder: /app
Container folder: /app
Any changes made inside the container’s /app folder reflect on the host, and vice versa.
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.
Very simple setup.
Great for development environments where you want to see changes instantly.
Must specify exact host directory path.
Works only on that specific host.
No built-in management via Docker CLI.
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.
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.
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.
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.
| 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 |
# 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
docker volume create myvol
docker run -d --mount source=myvol,target=/app nginx
The container nginx now uses /app linked to myvol.
Any file written in /app is persisted.
docker inspect <container_id>
You’ll see:
"Mounts": [
{
"Type": "volume",
"Name": "myvol",
"Source": "/var/lib/docker/volumes/myvol/_data",
"Destination": "/app",
"Mode": "rw"
}
]
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
-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.
| 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. |