πŸ“¦ Docker Volumes – A2Z Storage Guide 🐳



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