Docker - Day5


Welcome to Day 5 of Docker series! So far, we’ve learned how to build, run, and network Docker containers. But what happens when your container needs to persist data?
In today’s blog, we’ll dive into:
Why we need volumes
Types of Docker volumes
How volumes work under the hood
How to create and manage volumes
Best practices and real-world use cases
Why Do We Need Volumes?
By default, any data created inside a container is ephemeral—meaning it’s gone when the container is removed. But real-world apps (like databases, logging systems, etc.) need to persist data even after containers are stopped or recreated.
Volumes provide a solution by decoupling storage from the container lifecycle.
Docker Volumes
A Docker Volume is a storage mechanism managed by Docker itself, used to persist and share data across containers.
Think of volumes as a safe, independent space on your host (or remote) that lives outside of containers—but is accessible from them.
Types of Docker Volumes
Docker supports different types of volumes and mounts that allow containers to store and persist data.
1. Named Volumes
Volumes explicitly created and managed by Docker.
docker volume create myvolume
- Location on Host:
/var/lib/docker/volumes/
✅ Use Cases:
Databases
Application persistent storage
Easy sharing between containers
2. Anonymous Volumes
Volumes Docker creates without a specified name.
docker run -v /app/data myimage
Volume name: Randomly generated (e.g.,
6c5d1f3a99d59...
)Difficult to manage manually
⚠️ Use Cases:
Quick testing
When you don’t need to reuse or manage volume outside the container
3. Bind Mounts
Mounts a specific path on the host to a path inside the container.
docker run -v /host/path:/container/path myimage
- Host-controlled, unlike volumes that Docker manages.
⚠️ Use Cases:
Development (e.g., live sync code)
Mounting configuration files from the host
⚠️ Limitations:
Less portable
Can break container isolation
Prone to permission issues
4. tmpfs Mounts
Mounts a temporary file system in memory only (never written to disk).
docker run --tmpfs /app/tmp myimage
✅ Use Cases:
Store sensitive data (e.g., tokens, keys)
Temporary files like caches
Performance-critical use cases (in-memory speed)
⚠️ Limitations:
- Data disappears after container stops
5. Volume Plugins (Third-Party Volumes)
Custom drivers that integrate with external storage backends (e.g., AWS EBS, NFS, NetApp).
docker volume create \
--driver rexray/ebs \
--name my-ebs-volume
✅ Use Cases:
Enterprise-scale, distributed, or cloud storage
Kubernetes/Swarm with persistent storage
Summary Table
Type | Managed By | Persistent | Host Path Access | Best For |
Named Volume | Docker | ✅ Yes | ❌ No | Databases, reusable data |
Anonymous Volume | Docker | ✅ Yes | ❌ No | Quick throwaway testing |
Bind Mount | User/Host | ✅ Yes | ✅ Yes | Dev environments |
tmpfs Mount | Memory | ❌ No | ❌ No | Temporary sensitive data |
Volume Plugin | External | ✅ Yes | Depends | Cloud storage, NFS, etc. |
How to Work with Docker Volumes
1. Create a Volume
This creates a volume named mydata.
docker volume create mydata
2. Use Volume in a Container
Here, the MySQL container will store all data in the volume mydata
—not inside the container itself.
docker run -d \
--name mydb \
-v mydata:/var/lib/mysql \
mysql
3. Inspect Volumes
This shows mount point, driver info, usage, etc.
docker volume inspect mydata
4. Remove a Volume
docker volume rm mydata
Use Case: Persistent PostgreSQL
docker volume create pgdata
docker run -d \
--name pg \
-e POSTGRES_PASSWORD=mysecret \
-v pgdata:/var/lib/postgresql/data \
postgres
Stop and remove the container:
docker rm -f pg
Start a new one and reuse the volume:
docker run -d \
--name pg2 \
-e POSTGRES_PASSWORD=mysecret \
-v pgdata:/var/lib/postgresql/data \
postgres
You’ll find your database is still intact. 🎉
Best Practices for Docker Volumes
Use named volumes for clarity and reusability.
Avoid bind mounts in production (security risks).
Version control your volume definitions in
docker-compose.yml
.Backup volumes using
docker run --rm -v mydata:/data busybox tar
.
Docker Compose + Volumes Example
version: "3.8"
services:
db:
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Run with:
docker compose up -d
✅ Follow me for more hands-on content
💬 Share your volume use case in the comments
🚀 Happy Dockering!
Subscribe to my newsletter
Read articles from Harika Devulapally directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Harika Devulapally
Harika Devulapally
DevOps Engineer with expertise in AWS, Docker, Kubernetes, Terraform, and Ansible. Focused on automation, performance, and security