Named Volumes
quicknotes' notes must survive every upgrade. The standard Docker answer is a volume: storage that Docker creates and manages, which lives independently of any container and can be attached to a new one in seconds.
The rule
A volume outlives every container that mounts it.
A named volume is a storage area managed by Docker. On Linux its files sit under Docker's own data directory, but you never need to go there: you refer to it only by name. You attach it to a path inside the container, and everything written under that path goes into the volume instead of the writable layer.
docker volume create notes-data
docker run -d --name notes -p 8080:5000 \
-v notes-data:/app/data \
quicknotes:1.1The -v flag reads volume-name : path-in-container. The longer --mount form says the same thing more explicitly:
--mount type=volume,source=notes-data,target=/app/dataIf the named volume does not exist yet, docker run creates it. The managing commands:
docker volume ls
docker volume inspect notes-data
docker volume rm notes-data # only when no container uses it
docker volume prune # removes unused volumes; read the prompt carefullyUseful behaviour to know:
- Upgrading is now safe: remove the
quicknotes:1.1container, runquicknotes:1.2with the same-v, and every note is there. - Sharing: several containers can mount the same volume at once. The app must handle two writers safely.
- Pre-filling: when an empty named volume is mounted onto a folder that already has files in the image, Docker copies those files into the volume first.
- Deleting a container never deletes a named volume. Data goes only when you delete the volume deliberately.
The shortcut
Read every -v by its left side. A plain name (notes-data) means a Docker-managed volume. A path starting with /, ./ or $PWD means a folder on your machine: a bind mount, which is the next lesson.
Before
notesrunsquicknotes:1.1with-v notes-data:/app/data. The notes live in the volume.
Drills
Answer fast — trust the rule.
In -v notes-data:/app/data, what is notes-data?
You run docker rm -f notes. What happens to the notes-data volume it used?
Which command deletes the volume notes-data, once no container uses it?
An empty named volume is mounted at /app/data, where the image already has a welcome.txt. What does the container see?
Remember
- A volume outlives every container that mounts it; delete it only on purpose.
- Shortcut: read the left side of
-v: a name is a volume, a path is a bind mount. - Trap:
docker rmnever removes named volumes, anddocker volume pruneremoves unused ones in bulk.
