Skip to the workspace
Pebellslearn by building
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.

docker / Named Volumes

Explore each scene at your own pace.

01 · The rule

The rule

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.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.