DEV Community

Yash Sonawane
Yash Sonawane

Posted on

πŸ›³οΈ Docker Series: Episode 6 β€” Docker Volumes: The Secret to Saving Your Data

🎬 "Ever wondered why your app’s data disappears when you stop a container? In this episode, we uncover Docker Volumes β€” your key to persistent, reliable, and sharable data storage."


🧠 Why Data Disappears in Docker

By default, Docker containers are ephemeral β€” which means:

  • When the container stops, its filesystem is wiped.
  • All your app logs, databases, and uploads? Gone. 🫠

This is great for clean environments… but not if you need data to stick around.

Enter: Docker Volumes.


πŸ“¦ What Is a Docker Volume?

A volume is a special folder outside the container, managed by Docker.

Think of it like:

  • A USB drive you plug into your container
  • When the container is deleted, the USB (volume) stays intact

You can re-attach it, share it across containers, and even back it up.


πŸ”§ Using Volumes in Real Projects

πŸ§ͺ Example 1: MySQL Database Persistence

docker run -d \
  --name mysql \
  -e MYSQL_ROOT_PASSWORD=mysecret \
  -v mysql-data:/var/lib/mysql \
  mysql:latest
Enter fullscreen mode Exit fullscreen mode

βœ… This creates a volume mysql-data that stores the DB.
Even if you delete the container, the database stays.


πŸ”¨ Common Volume Commands

πŸ“ List Volumes

docker volume ls
Enter fullscreen mode Exit fullscreen mode

πŸ” Inspect a Volume

docker volume inspect mysql-data
Enter fullscreen mode Exit fullscreen mode

❌ Remove a Volume

docker volume rm mysql-data
Enter fullscreen mode Exit fullscreen mode

🧹 Prune Unused Volumes

docker volume prune
Enter fullscreen mode Exit fullscreen mode

🧱 Volume Types (Simplified)

Type Use Case
Named Volume Long-term data (MySQL, logs, uploads)
Bind Mount Dev mode (sync local folders)

πŸ§ͺ Example 2: Dev Sync with Bind Mount

docker run -d \
  -v $(pwd):/app \
  -w /app \
  node:18 \
  npm start
Enter fullscreen mode Exit fullscreen mode

βœ… Any change you make locally reflects inside the container in real-time.


πŸ› οΈ Tips for Volume Mastery

  • Use named volumes in production
  • Use bind mounts for live development
  • Clean up unused volumes regularly

πŸ“¦ Up Next: Docker Networking Demystified

In Episode 7, we’ll explore:

  • How containers talk to each other
  • Networks, bridges, ports, and real examples (Node + DB)

πŸ’¬ Let’s Talk

Did you try volumes in your app yet? Still confused between named volumes and mounts?

Drop a question β€” I’d love to help you debug or design the best volume setup.

❀️ If this episode helped keep your data safe, give it a like or share it with someone building with Docker.

🎬 Next: β€œDocker Networking β€” How Containers Talk Behind the Scenes”

Top comments (0)