Docker Volumes & Storage – Persisting Data in Containers
Learn how to persist data in Docker using volumes and prevent data loss in containers.
1. Introduction
By default, when a Docker container stops, all its data is lost. 😱
Imagine running a database inside a container—if the container stops, all stored data disappears!
Docker volumes solve this problem by providing persistent storage, ensuring data is not lost when a container restarts.
In this guide, you’ll learn why containers lose data, how to use Docker volumes, and how to backup & restore data easily. 🚀
2. Why Do Containers Lose Data After Stopping?
Docker containers are designed to be ephemeral (temporary). This means:
✅ When a container starts, it gets a fresh filesystem.
❌ When it stops, all unsaved data is deleted.
Example:
Run a temporary Ubuntu container:
docker run -it ubuntu
Inside the container, create a file:
echo "Hello, Docker!" > myfile.txt
exit
Now restart the container and check the file:
docker start <container_id>
docker exec -it <container_id> ls
Oops! The file is gone. 😲
3. Introduction to Docker Volumes
A Docker volume is a way to store data outside the container, so it remains even after the container stops.
Creating a Volume
docker volume create mydata
Using the Volume in a Container
docker run -d --name mycontainer -v mydata:/app busybox
This mounts mydata
inside the container at /app
.
Now, any data stored in /app
will persist even if the container stops or is removed. 🎉
4. How to Create & Use Named Volumes
Named volumes are the most common way to store persistent data.
Step 1: Create a Volume
docker volume create myvolume
Step 2: Run a Container with the Volume
docker run -it --rm -v myvolume:/data ubuntu bash
Now, any files inside /data
will be saved permanently.
Step 3: Verify the Data Persists
1️⃣ Create a file inside the container:
echo "Hello, volume!" > /data/myfile.txt
exit
2️⃣ Start a new container and check if the file still exists:
docker run -it --rm -v myvolume:/data ubuntu ls /data
✅ The file is still there! 🎉
5. Bind Mounts vs Named Volumes – Key Differences
Feature | Named Volumes | Bind Mounts |
Storage Location | Managed by Docker | Specific directory on host |
Flexibility | More portable | More control over path |
Best For | Persistent container data | Sharing host files with container |
Example of a Bind Mount
To share a local folder with a container:
docker run -d -v /my/local/folder:/app busybox
This links the host folder /my/local/folder
to /app
inside the container.
6. Backing Up & Restoring Volumes
Backup a Volume
docker run --rm -v myvolume:/data -v $(pwd):/backup busybox tar -czf /backup/backup.tar.gz -C /data .
This creates a compressed backup (backup.tar.gz
).
Restore a Volume
docker run --rm -v myvolume:/data -v $(pwd):/backup busybox tar -xzf /backup/backup.tar.gz -C /data
✅ Your volume is restored! 🎉
7. Conclusion
Docker volumes make it easy to store and persist data, preventing data loss when containers stop.
Now you know how to:
✅ Use named volumes for persistent storage
✅ Understand bind mounts vs named volumes
✅ Backup & restore Docker volumes
Try using Docker volumes in your projects and keep your data safe! 🚀