🐳 Docker Learning Hub
Lesson 4 • Volumes and persistent data
Lesson 4

Docker volumes and persistent data

This page answers one of the most important beginner questions: what happens to data when a container is removed, and how do volumes solve that problem?

Main problem

Container storage is not something you should depend on for important long-term data.

Main solution

A Docker volume stores data separately from the container so the data can remain even when the container changes.

Main result

Your app can be replaced or restarted without automatically losing useful stored data.

Without a volume

Container stores data inside itself
Container gets removed
Data may disappear with it

With a volume

Container writes data to a volume
Container gets removed
Volume still remains with the data

What is a Docker volume?

A Docker volume is a Docker-managed storage area used to keep persistent data outside the container’s temporary writable layer.

Simple memory line: containers run apps, volumes keep data.

Layman example

Think of a container like a hotel room and a volume like a locker outside that room. You can leave the room, but the locker can still keep your things.

Create and inspect a volume

docker volume create mydata docker volume ls docker volume inspect mydata

Mount a volume into a container

docker run -d -v mydata:/app/data nginx

This connects the Docker volume named mydata to the folder /app/data inside the container.

Named volume vs bind mount

Named volume
Managed by Docker, good for persistent application data.
Bind mount
Maps a real folder from your computer into the container, useful in development.

Examples

-v mydata:/app/data -v /Users/yourname/project:/app

The first is a named volume. The second is a bind mount from the host machine.

Persistence example

docker volume create notes_data docker run -it --name notes-box -v notes_data:/data busybox sh echo "Hello volume" > /data/notes.txt exit docker rm notes-box docker run -it --name notes-box-2 -v notes_data:/data busybox sh cat /data/notes.txt
If you still see Hello volume in the second container, that proves the data stayed in the volume and not only in the first container.

What you learned in Lesson 4

Container data is not the same as persistent storage
Important data should not depend only on the container layer.
Volumes protect useful data
They stay separate from the container lifecycle.
Named volumes and bind mounts are different
One is Docker-managed, the other uses a host folder directly.
Persistence is critical for real apps
Databases, uploads, and saved files often rely on volumes.

Next page: Lesson 5 explains how containers communicate through Docker networks.