Main problem
Container storage is not something you should depend on for important long-term 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?
Container storage is not something you should depend on for important long-term data.
A Docker volume stores data separately from the container so the data can remain even when the container changes.
Your app can be replaced or restarted without automatically losing useful stored data.
A Docker volume is a Docker-managed storage area used to keep persistent data outside the container’s temporary writable layer.
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.
docker volume create mydata
docker volume ls
docker volume inspect mydata
docker run -d -v mydata:/app/data nginx
This connects the Docker volume named mydata to the folder /app/data inside the container.
-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.
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
Hello volume in the second container, that proves the data stayed in the volume and not only in the first container.
Next page: Lesson 5 explains how containers communicate through Docker networks.