docker compose rejects a service that uses a volume nobody declared
service "db" refers to undefined volume pgdata: invalid compose projectShort answer
A short volume entry like data:/var/lib/data names a volume called data. Compose then expects a matching entry under the top-level volumes key. If it is missing, misspelled or indented wrongly, Compose stops. Declare the volume, fix the name, or write a host path if you meant a bind mount.
Fix it fast
1Find out which cause you have
List the volumes Compose actually found in the file. If the one you use is not in the list, the declaration is missing or nested in the wrong place.
docker compose config --volumes docker compose config --quiet2Match what you see, then jump to its fix
- 1
The services block uses name:/path but there is no volumes: block at the left edge of the fileThe named volume is not declared at the top levelFix - 2
A volumes: block exists, but it sits under services or the spelling differs from the service's entryThe top-level volumes block is indented, or the name does not matchFix - 3
The left side of the mapping is a plain word such as data or app, not a pathYou meant a host folder, not a named volumeFix
- 1
What does the error look like?
service "db" refers to undefined volume pgdata: invalid compose projectUndefined volume with Docker Compose
Cause 1 of 3
The named volume is not declared at the top level
1Check
The docs say a named volume has to be declared in the top-level volumes element before several services can reuse it, and a service gets access only through its own volumes attribute.
grep -n -E '^volumes:|^services:|^ *- *[A-Za-z0-9_.-]+:/' docker-compose.yml2Fix
Add the volume under a top-level volumes key. An empty entry is enough: the engine's default settings are used and Compose creates it on the first up.
services:
db:
image: postgres:17
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:3Verify
The volume now appears in the list.
docker compose config --volumesConfirmed by docs.docker.com
Cause 2 of 3
The top-level volumes block is indented, or the name does not match
1Check
volumes must be a sibling of services, at the same indentation. Names are matched exactly, so pgdata and pg_data are two different volumes.
docker compose config --volumes
grep -n -E 'volumes:|pg' docker-compose.yml2Fix
Move the block to the left edge and use one spelling in both places.
services:
db:
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:3Verify
Compose parses the file without the error.
docker compose config --quiet && echo OKConfirmed by docs.docker.com
Cause 3 of 3
You meant a host folder, not a named volume
1Check
The left side can be a volume name or a host path. To avoid mixing them up, the docs say relative paths should always begin with . or .., so a plain word is read as a volume name.
grep -n -E '^\s+- [A-Za-z0-9_.-]+:/' docker-compose.yml2Fix
Start the left side with ./ or an absolute path, and remove the entry from the top-level block if you added it only to silence the error.
services:
web:
volumes:
- ./data:/var/lib/data3Verify
The file parses and the folder shows inside the container.
docker compose config --quiet && echo OK
docker compose run --rm web ls /var/lib/dataConfirmed by docs.docker.com
How do I stop it coming back?
- Run docker compose config --quiet before docker compose up.
- Write ./ in front of every host path so a path is never mistaken for a volume name.
- Declare every named volume in the same commit that first uses it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
docker compose up refuses to start because an external volume does not exist
Volume my_data declared as external, but could not be found
A volume marked external: true is never created by Compose.
3 causes, each with a fixServers
docker compose says a service depends on an undefined service
docker compose up fails before any container starts
Compose could not find the service named in depends_on in the model it loaded.
3 causes, each with a fixServers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fix