Sub topic 11
Declaring volumes in Docker Compose
This chapter assumes you can run containers with docker run, read basic YAML, and know the difference between named volumes and bind mounts. Compose lets you describe the same storage in a file instead of in long command lines.
Two places volumes appear
A Compose file uses volumes in two places:
- Under a service,
volumeslists what to mount into that service's containers. - At the top level,
volumesdeclares the named volumes that services can use.
The short syntax for a service entry is SOURCE:TARGET[:MODE]. Compose decides the mount type from the source. A plain name such as pgdata is a named volume. A path that starts with ., /, or ~ is a bind mount.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./site:/usr/share/nginx/html:ro
volumes:
pgdata:Here db stores its data in the named volume pgdata. The web service bind-mounts the ./site directory from the host and marks it read-only with :ro. Relative paths like ./site are resolved against the directory that holds the Compose file.
Why the top-level declaration matters
A named volume used by a service must be declared at the top level. Without it, Compose stops with an error saying the service refers to an undefined volume. A bind mount needs no declaration, because it points at a host path and not at a Docker-managed object.
The empty entry pgdata: asks for the defaults: the local driver and a name that Compose builds for you.
Naming and project scope
Compose prefixes volume names with the project name, which defaults to the name of the directory that holds the Compose file. If the project is shop, pgdata becomes shop_pgdata. You can see this with docker volume ls.
This prefix lets two projects use the same short name without sharing data. If you want a fixed name, or want to use a volume that already exists, set name or external:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
external: true
name: pgdataWith external: true, Compose never creates or deletes the volume. It fails if the volume does not exist, so run docker volume create pgdata first.
The long syntax
The long syntax spells out each field. It is easier to read and avoids mistakes with colons.
services:
web:
image: nginx:alpine
volumes:
- type: bind
source: ./site
target: /usr/share/nginx/html
read_only: trueLifecycle and common errors
Named volumes outlive the containers that use them. These commands behave differently:
| Command | Effect on named volumes |
|---|---|
| `docker compose down` | Removes containers and networks. Keeps volumes. |
| `docker compose down -v` | Also removes the named volumes declared in the file. |
| `docker compose up` | Creates missing volumes and reuses existing ones. |
Watch for these problems:
- Data seems to vanish. You changed the project name or directory, so Compose created a new, empty
projectname_pgdata. The old volume is still there. - Undefined volume error. The name is used in a service but missing from the top-level
volumeskey. - A relative path became a volume. Writing
site:/usr/share/nginx/htmlcreates a named volume calledsite. Write./sitefor a bind mount.
Exercise
A Compose file has a service with volumes: - data:/var/lib/app, but no top-level volumes key. What happens when you run docker compose up, and how do you fix it?
Show answer
Compose reports an error that the service refers to an undefined volume data. Fix it by adding a top-level volumes: key with an entry data:. If you meant a host directory, write ./data:/var/lib/app instead, which needs no declaration.
Exercise
You run docker compose down -v in a project named shop that declares pgdata. Which volume is deleted, and would external: true change the result?
Show answer
The volume shop_pgdata is deleted. With external: true, Compose does not manage the volume's lifecycle, so down -v would leave it in place.