Docker Volumes & Storage – Persisting Data in Containers

Pawan LuhanaPawan Luhana
3 min read

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

FeatureNamed VolumesBind Mounts
Storage LocationManaged by DockerSpecific directory on host
FlexibilityMore portableMore control over path
Best ForPersistent container dataSharing 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! πŸš€

0
Subscribe to my newsletter

Read articles from Pawan Luhana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pawan Luhana
Pawan Luhana