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)