Sub topic 4
Managing volumes: create, inspect, remove, prune
This chapter assumes you can run containers with docker run, you know that a named volume outlives its container, and you are comfortable in a Linux shell. Here you will learn the docker volume commands that control a volume's lifecycle, and the errors you are most likely to hit.
The volume lifecycle
A named volume is managed separately from containers. You create it, containers attach to it, and it stays until you delete it. Removing a container never removes a named volume.
Create and list
You can create a volume explicitly. Docker also creates a named volume automatically the first time a container mounts a name that does not exist yet.
docker volume create pgdata
docker volume lsdocker volume ls prints the driver and name of each volume. The default driver is local, which stores data on the Docker host.
Creating a volume that already exists is not an error. Docker returns the existing volume, so scripts can run docker volume create safely more than once.
Inspect
docker volume inspect prints a volume's metadata as JSON.
docker volume inspect pgdataThe output includes these fields:
Name: the volume name.Driver: usuallylocal.Mountpoint: where the data lives on the host, often under/var/lib/docker/volumes/pgdata/_dataon Linux.CreatedAtandLabels: when it was created and any metadata you attached.
To read one field, use a Go template:
docker volume inspect --format '{{ .Mountpoint }}' pgdataRemove
Remove a volume by name once nothing uses it:
docker rm -f db2
docker volume rm pgdatadocker volume rm fails if any container, running or stopped, still references the volume. The error looks like volume is in use. Run docker ps -a --filter volume=pgdata to find the containers that hold it, remove them, then retry.
Prune
docker volume prune removes volumes that no container references. It asks for confirmation before deleting.
docker volume pruneBe careful with the definition of "unused". A volume used by a stopped container is still referenced, so it is kept. A volume whose container you already deleted is unused, so it is deleted, even if it holds a database you wanted.
Recent Docker Engine versions (23.0 and later) prune only anonymous volumes by default. To include named volumes, add --all. You can also narrow the selection with a filter:
docker volume prune --filter "label!=keep"
docker volume prune --allUse labels to protect important volumes:
docker volume create --label keep=true pgdataCommon errors
| Symptom | Cause | Fix |
|---|---|---|
| `volume is in use` | A container, even a stopped one, references the volume | Remove the container with `docker rm`, then remove the volume |
| Database starts empty after a restart | The volume name was misspelled, so Docker created a new empty volume | Run `docker volume ls` and correct the name |
| Prune deleted data you wanted | The volume's container had already been removed | Label important volumes and filter on the label; keep backups |
| Cannot find the mountpoint on the host | Docker Desktop runs in a VM | Mount the volume in a temporary container to browse it |
Exercise
You run docker rm -f db and then docker volume prune, answering yes. The pgdata volume was created with docker volume create pgdata and was used only by db. Is the data still there on Docker Engine 23.0 or later, and why?
Show answer
Yes, on those versions, because docker volume prune removes only anonymous volumes unless you add --all, and pgdata is a named volume. On older versions it would have been deleted, since no container referenced it after db was removed.
Exercise
docker volume rm pgdata reports that the volume is in use, but docker ps shows no running containers. What do you check next?
Show answer
Stopped containers still reference the volume. Run docker ps -a --filter volume=pgdata, remove those containers with docker rm, and run docker volume rm pgdata again.