Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Persistence  ›  Lesson

Named Volumes

Volume Mounts 14 minA named volume is storage created and managed by Docker that outlives any container, mounted at a path inside the container with -v name:/path or --mount.

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.1

The -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/data

If 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 carefully

Useful behaviour to know:

  • Upgrading is now safe: remove the quicknotes:1.1 container, run quicknotes:1.2 with 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.

Upgrading quicknotes without losing notesStep 1 of 4
  1. Before

    notes runs quicknotes:1.1 with -v notes-data:/app/data. The notes live in the volume.

Drills

Answer fast — trust the rule.

quick drill

In -v notes-data:/app/data, what is notes-data?

quick drill

You run docker rm -f notes. What happens to the notes-data volume it used?

quick drill

Which command deletes the volume notes-data, once no container uses it?

quick drill

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 rm never removes named volumes, and docker volume prune removes unused ones in bulk.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainKeep the notes

Change the quicknotes setup so notes stored under /app/data survive container removal and image upgrades. Give the commands, explain the lifecycle of the volume, and say how you would finally delete the data.