📦 Docker Volumes Explained – The Complete Beginner-to-Pro Guide

Table of contents
- 🧐 1. What is a Docker Volume?
- 🎯 2. Why Do We Need Docker Volumes?
- 📍 3. Where to Use Docker Volumes?
- 🗂️ 4. Types of Docker Volumes
- 🛠️ 5. Docker Volume Commands – Complete Cheat Sheet
- ⚙️ 6. Real Example – Node.js App with Docker Volume
- 💡 7. Best Practices
- 🎯 8. Conclusion
- 🔗 What’s Next?
- 📝 Additional Resources
- ✍️ Author’s Note
- 💬 Have Questions or Suggestions?

When you start learning Docker, you quickly notice something strange: You spin up a container, create some files inside it… and then poof! — the moment you remove that container, everything is gone.
Why does this happen? Because containers are designed to be temporary — think of them as disposable environments.
This is perfect for running applications but terrible for keeping persistent data like:
- Databases (MySQL, MongoDB, PostgreSQL, etc.)
- Uploaded files (images, PDFs, videos)
- App configuration files and more.
So… how do we make our data survive container restarts and deletions? Answer: Use Docker Volumes.
🧐 1. What is a Docker Volume?
A Docker Volume is a special storage space that exists outside the container’s filesystem but is still accessible from the container.
Think of it like this:
Without Volume:
[Container Filesystem] → Data is deleted when container is removed ❌
With Volume:
[Container] → Data stored in [Volume] → Data survives even if container is removed ✅
Key facts:
- Volumes are managed by Docker.
- They can be shared between containers.
- They exist independently of containers.
🎯 2. Why Do We Need Docker Volumes?
Let’s imagine a MySQL container:
- You create a database and insert data.
- You stop and remove the container.
- Your database? Gone forever. 😭
With volumes:
- You store the database files in a volume.
- You remove the container.
- You start a new MySQL container with the same volume → your database is still there! 🎉
Benefits of Docker Volumes:
- Persistence – Keep data safe between container restarts.
- Sharing – Multiple containers can read/write to the same data.
- Backup/Restore – Easy to copy volumes for safety.
- Performance – Volumes are optimized for container storage.
- Portability – Move volumes between environments.
📍 3. Where to Use Docker Volumes?
You’ll use volumes whenever your containerized app:
- Needs to store important data (databases, media uploads, logs).
- Needs to share data between containers (example: a backend and frontend sharing config files).
- Needs fast I/O (volumes are optimized for performance).
- Requires isolation (containers don’t mess with each other’s storage unless you allow it).
🗂️ 4. Types of Docker Volumes
Docker supports three main ways to persist data.
4.1 Named Volumes
- Created and managed by Docker.
- You give them a name.
- Docker stores them in
/var/lib/docker/volumes/
on the host. - Best for production and long-term storage.
Example:
docker volume create mydata
docker run -d --name app -v mydata:/app/data node:18
Here:
mydata
→ named volume/app/data
→ folder inside the container
4.2 Anonymous Volumes
- Created automatically when you mount a path without a name.
- Docker gives them a random name (hard to track).
- Best for temporary storage.
Example:
docker run -d -v /app/data node:18
Problem: You don’t know the volume’s name unless you inspect it.
4.3 Bind Mounts
- Instead of letting Docker manage storage, you link a folder from your host machine to the container.
- Perfect for development because changes on your host reflect instantly inside the container.
Example:
docker run -d -v /home/user/myfolder:/app/data node:18
/home/user/myfolder
→ folder on your host/app/data
→ folder inside container
🛠️ 5. Docker Volume Commands – Complete Cheat Sheet
Command | Description |
docker volume create myvol | Create a named volume |
docker volume ls | List all volumes |
docker volume inspect myvol | Show details of a volume |
docker volume rm myvol | Remove a specific volume |
docker volume prune | Remove all unused volumes |
docker run -v myvol:/path | Use a named volume |
docker run -v /host/path:/path | Use a bind mount |
docker run -v /path | Create an anonymous volume |
⚙️ 6. Real Example – Node.js App with Docker Volume
Let’s make a Node.js logging app that stores logs in a Docker volume.
Step 1: Project Structure
docker-volume-demo/
│── app.js
│── package.json
│── Dockerfile
Step 2: app.js
const fs = require('fs');
const path = require('path');
const logDir = path.join(__dirname, 'logs');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const logFile = path.join(logDir, 'app.log');
fs.appendFileSync(logFile, `App started at ${new Date()}\n`);
console.log('Hello from Docker Volumes!');
Step 3: package.json
{
"name": "docker-volume-demo",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js"
}
}
Step 4: Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
Step 5: Build the image
docker build -t node-volume-demo .
Step 6: Run with a Named Volume
docker run --name nodeapp -v logs_data:/app/logs node-volume-demo
Step 7: Check the Volume
docker volume ls
docker volume inspect logs_data
Step 8: Test Persistence
docker rm -f nodeapp
docker run --name nodeapp2 -v logs_data:/app/logs node-volume-demo
✅ You’ll see logs from both runs stored in the volume.
💡 7. Best Practices
- Use named volumes for production.
- Use bind mounts for local development.
Regularly clean unused volumes with:
docker volume prune
Backup critical volumes:
docker run --rm -v mydata:/data -v $(pwd):/backup busybox tar czf /backup/data.tar.gz /data
🎯 8. Conclusion
Docker Volumes are the secret sauce that lets containers handle persistent and shareable data. Without them, containers are like disposable coffee cups — use once, then throw away. With volumes, they’re more like reusable mugs — they keep the good stuff safe, even after many uses. ☕
🔗 What’s Next?
You might want to explore:
- Docker Command Cheat Sheet
- Everything About Docker Compose
- How to Build Docker Images with Dockerfile
📝 Additional Resources
✍️ Author’s Note
This blog is a comprehensive guide to Docker Volumes, but the best way to learn is by doing! Try building your own app using the examples provided. If you have any questions or suggestions, feel free to reach out.
💬 Have Questions or Suggestions?
Drop a comment below or connect with me on LinkedIn or GitHub. Let’s make apps faster together! 🚀
Subscribe to my newsletter
Read articles from KUNTAL MAITY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

KUNTAL MAITY
KUNTAL MAITY
I’m a passionate Full-Stack Developer who loves building performance-driven web and mobile applications. I work primarily with JavaScript, React, Next.js, Node.js, and MongoDB, and I enjoy simplifying complex concepts into developer-friendly tutorials. On this blog, you'll find hands-on guides, real-world projects, and developer insights—all aimed at helping you level up your coding skills and build production-ready apps. Whether you're into backend performance, frontend polish, or full-stack architecture, there's something here for you! Let’s learn, build, and grow together. 💻✨