Docker Volumes & Storage β Persisting Data 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! π
Subscribe to my newsletter
Read articles from Pawan Luhana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
