This content originally appeared on DEV Community and was authored by Darshan Vasani
π¦ Docker Volumes β A2Z Storage Guide π³
π What is a Docker Volume?
A Docker volume is a persistent storage mechanism managed by Docker outside the container filesystem.
β
Volumes survive container restarts
β
Volumes are managed by Docker
β
Theyβre perfect for storing databases, logs, user uploads, and config files
π Real-World Analogy:
π§³ Think of a volume as a USB stick plugged into your container.
- You can eject the container π£
- The USB (volume) still has all your files πΎ
π§ Why Use Volumes?
β Benefit | π¬ Why Itβs Awesome |
---|---|
Persistent Storage | Data stays even after container dies |
Decoupled | Separate from container logic |
Shared Access | Mount same volume into multiple containers |
Backup Friendly | Easy to archive/export |
Safe from image rebuilds | Won't be deleted accidentally |
π Types of Docker Volume Mounts
Type | Syntax Example | Use Case |
---|---|---|
π³ Named Volume | -v my-volume:/app/data |
Default, managed by Docker |
ποΈ Host Bind | -v /host/folder:/container/folder |
Use host machine's file system |
π§ͺ Anonymous | -v /app/data |
Randomly named, temporary |
π§ 1. Using a Named Volume
docker volume create mydata
docker run -d \
--name db \
-v mydata:/var/lib/mysql \
mysql
β
The data is stored in:
/var/lib/docker/volumes/mydata/_data
ποΈ 2. Attaching Host Folders (Bind Mounts)
docker run -d \
--name webapp \
-v /home/user/project:/usr/src/app \
node:alpine
β Mounts a host folder directly inside the container.
β οΈ Bind mounts are powerful but risk exposing sensitive host files if misused.
π₯ Differences: Volume vs Bind Mount
Feature | Volume (Docker-managed) | Bind Mount (Host folder) |
---|---|---|
Managed by Docker | β Yes | β No |
Host portability | β Portable | β Host-specific |
Data safety | β Isolated | β Depends on host path |
Security | β Better | β οΈ Potentially risky |
π 3. Share Volume Between Multiple Containers
π§ͺ Example:
docker volume create shared-data
docker run -d --name writer \
-v shared-data:/data \
busybox sh -c "echo hello > /data/file.txt && sleep 9999"
docker run --rm --name reader \
-v shared-data:/data \
busybox cat /data/file.txt
π¦ Both writer
and reader
share the same volume.
π Read-only Volume Mount
Prevent writing:
-v mydata:/app/data:ro
β Makes volume read-only inside the container!
π¦ Volume Lifecycle Commands
π§ Command | π¬ What It Does |
---|---|
docker volume create <name> |
Create a volume |
docker volume ls |
List all volumes |
docker volume inspect <name> |
View volume details |
docker volume rm <name> |
Delete volume |
docker volume prune |
Delete all unused volumes |
π§ͺ Using Volumes in docker-compose.yml
version: "3.9"
services:
db:
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
β
Docker creates & manages the pgdata
volume
π§ Volume Naming Tip
- Named volumes persist and can be reused by name
- Anonymous volumes are created automatically and can be hard to track
docker run -v /data nginx # Anonymous volume
docker run -v myvol:/data nginx # Named volume β
π€ Backing Up & Restoring Volumes
π§³ Backup:
docker run --rm \
-v myvolume:/data \
-v $(pwd):/backup \
busybox tar czvf /backup/backup.tar.gz /data
β»οΈ Restore:
docker run --rm \
-v myvolume:/data \
-v $(pwd):/backup \
busybox tar xzvf /backup/backup.tar.gz -C /
π¨ Volume Gotchas to Avoid
β οΈ Mistake | π₯ Problem |
---|---|
Not naming volumes | Hard to manage & reuse |
Mixing bind mount with sensitive paths | Potential host damage |
Forgetting to prune | Unused volumes pile up |
Overwriting app folder with empty volume | App might not start! |
π§Ύ Summary Table
Type | Managed | Persistent | Use Case |
---|---|---|---|
π³ Named Volume | Docker | β | Safe default, backups |
ποΈ Bind Mount | Host | β | Mount local code |
π§ͺ Anonymous Volume | Docker | β οΈ Temporary | Quick test, not tracked |
β Final Takeaways
- π¦ Volumes are best practice for persistent, portable storage.
- π‘ Use named volumes for databases, uploads, logs.
- βοΈ Use bind mounts for local dev.
- π§ Share volumes to enable inter-container communication via files.
- π§½ Clean up with
docker volume prune
.
This content originally appeared on DEV Community and was authored by Darshan Vasani

Darshan Vasani | Sciencx (2025-07-20T08:13:39+00:00) π¦ Docker Volumes β A2Z Storage Guide π³. Retrieved from https://www.scien.cx/2025/07/20/%f0%9f%93%a6-docker-volumes-a2z-storage-guide-%f0%9f%90%b3/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.