Named volumes
Create, mount, inspect and remove named volumes; how an empty volume is pre-filled from the image; and why Postgres 18 moved its volume path.
Named volumes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A named volume is storage Docker creates and manages under its data root. It exists independently of any container and survives docker rm.
- Mount one with -v name:/path or the more explicit --mount type=volume,src=name,dst=/path. A volume that does not exist yet is created on first use.
- When an empty volume is mounted over a directory that has files in the image, Docker copies those files, with their owners and modes, into the volume first.
- A non-empty volume hides whatever the image had at that path. Changing the image does not update data already in the volume.
- docker volume prune removes only unused anonymous volumes by default. Named volumes need --all, which is a deliberate safety net.
- Mount the path the image actually uses. postgres:18 moved its data to /var/lib/postgresql and refuses to start with a mount at the old /var/lib/postgresql/data.
Storage that outlives the container
The previous lesson ended with data in the wrong place. A named volume is the right place: storage that Docker creates and tracks by name, separate from any container. Remove the container, start a new one from a newer image, mount the same volume, and the data is there.
The volumes page calls them “the preferred mechanism for persisting data generated by and used by Docker containers”. They are easier to back up than bind mounts, work the same on Linux and Windows, can be shared between containers, and on Docker Desktop they live inside the VM, which makes them much faster than a bind mount from the Mac or Windows filesystem.
Using one
docker volume create pgdata
docker run -d --name pg -e POSTGRES_PASSWORD=dev \
--mount type=volume,src=pgdata,dst=/var/lib/postgresql \
postgres:18
-v pgdata:/var/lib/postgresql does the same. The docs prefer --mount because it is explicit and supports every option: readonly, volume-subpath to mount one subdirectory of a volume, volume-nocopy, and driver options. If the volume does not exist, either form creates it.
The rest of the lifecycle:
docker volume ls
docker volume inspect pgdata # driver, labels, Mountpoint under /var/lib/docker/volumes/pgdata/_data
docker volume rm pgdata # refuses while a container uses it
docker volume prune # unused anonymous volumes only
docker volume prune --all # unused named volumes too
The prune default changed in Docker Engine 23: the volume prune reference says it only removes anonymous volumes unless you pass --all. The volumes guide still says plain docker volume prune removes all unused volumes, which is out of date. The safe default is worth knowing about, so nobody types --all without thinking. Cleaning up covers the other prune commands.
The Mountpoint from inspect is a real directory on the Docker host, but the docs call reading or writing it directly “unsupported, undefined behavior”. Go through a container instead, which is what backing up volumes does.
The first mount copies the image’s files
When you mount an empty volume over a directory that has files in the image, Docker copies those files into the volume first. When you mount a non-empty volume, it simply hides what the image had there, the way mounting a USB stick over /mnt hides what was in /mnt.
That copy keeps owners and modes. Docker’s daemon uses CopyDir from containerd’s continuity library, which applies the source directory’s mode and ownership to the volume’s root as well as to every file. This is why a non-root image works with a fresh named volume: postgres:18 creates /var/lib/postgresql owned by UID 999 with mode 1777 in its first RUN, I could see that in the layer from Docker Hub, and a new volume mounted there inherits it.
Two consequences:
- The copy happens once. Ship a new image with new files at that path and an existing volume never sees them. Do not put application code or static assets in a volume.
- Pass
volume-nocopywhen you want a truly empty volume.
Mount the right path
The path you mount must be where the image writes. postgres:18 is a recent example of that changing. From its image config:
ENV PGDATA=/var/lib/postgresql/18/docker
VOLUME [/var/lib/postgresql]
Through version 17, data lived in /var/lib/postgresql/data, and every tutorial mounts a volume there. In 18 the data directory includes the major version, so a future pg_upgrade --link can see old and new side by side on one mount. The entrypoint script checks for the old layout. If a volume is mounted at /var/lib/postgresql/data, it prints an error beginning:
Error: in 18+, these Docker images are configured to store database data in a
format which is compatible with "pg_ctlcluster" (specifically, using
major-version-specific directory names).
and exits. Upgrading a database’s major version is a data migration, not an image bump. The message says so and points at pg_upgrade. The general lesson: read an image’s documentation, or its VOLUME and data-directory variables, before deciding where to mount.
Volumes in Compose
In a Compose file a named volume is declared at the top level and referenced by services:
services:
db:
image: postgres:18
volumes:
- pgdata:/var/lib/postgresql
volumes:
pgdata:
Compose prefixes the name with the project, so the real volume is myproject_pgdata. docker compose down keeps it, and docker compose down -v deletes it, which is the reset button for a local database. The Compose file lesson has the rest.
Volume drivers let the same commands create volumes on NFS, CIFS or cloud storage. The local driver can mount NFS directly with --opt type=nfs. That is useful for sharing between hosts, with the caveats of the network filesystems lesson.
About the video
The embed is TechWorld with Nana’s “Docker Volumes explained in 6 minutes”, November 2019. Its chapters cover when you need volumes, what they are, the three types (host, anonymous and named) and volumes in a docker-compose file. It is from 2019, so it uses -v throughout and predates the prune change and Postgres 18’s new path. English was judged from the channel and the English caption track. JimShapedCoding’s video in deeper is longer, with every docker volume command.
What was run: the postgres:18 config, its first layer, which creates /var/lib/postgresql as 999:999 mode 1777, and its entrypoint script with the error text were fetched from Docker Hub’s registry API with curl and unpacked with tar. The copy behaviour was checked in the moby source (copyExistingContents) and containerd’s continuity CopyDir. The prune behaviour is from the docker/cli reference. No volume was created here, checked 2026-11-28.
Where this fits
Named volumes are Docker’s storage. Next, the other two kinds, a host directory and memory: bind mounts and tmpfs mounts.
Check yourself
You mount an empty named volume at /usr/share/nginx/html in an nginx container. What does the volume contain afterwards?
nginx's default index.html and 50x.html. Docker copies the image's contents at that path into an empty volume the first time it is mounted, unless you pass volume-nocopy.
You ship a new image with updated files in /app/static, which is a named volume. Users still see the old files. Why?
The volume was filled from the image only once, when it was empty. A non-empty volume hides the image's directory, so new image content never reaches it. Static assets usually should not live in a volume.
You run docker volume prune -f to free space and your database volume survives. Why?
Since Docker Engine 23, volume prune removes only unused anonymous volumes. Named volumes are kept unless you add --all.
A Compose file written for postgres:16 mounts pgdata:/var/lib/postgresql/data. After switching to postgres:18 the container exits with an error about old databases. Why?
postgres:18 keeps data under /var/lib/postgresql/18/docker and declares its volume at /var/lib/postgresql. Its entrypoint detects a mount at the old path and stops, since upgrading needs pg_upgrade, not a new image on old data.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.