service "db" refers to undefined volume "dbdata": invalid compose project
Fixes the docker-compose error `service "db" refers to undefined volume "dbdata": invalid compose project`. It explains the verified fix from a real resolved case, when it applies, and what to check first. Not for unrelated docker-compose failures or different error text.
Fix service "db" refers to undefined volume "dbdata": invalid compose project (docker-compose)
TL;DR
The service mounts a named volume that is missing from the top-level volumes: section. Add the volume name under top-level volumes: (an empty entry is enough). Bind mounts and bare paths never need this, only named volumes.
The error, verbatim:
service "db" refers to undefined volume "dbdata": invalid compose projectSteps
- Confirm which volume is undeclared:
docker compose configSuccess check: The error names the exact service and volume.
- Declare the volume at the top level of the compose file:
grep -n -A5 "^volumes:" docker-compose.ymlSuccess check: dbdata: now appears under top-level volumes:.
- Re-validate and bring the stack up:
docker compose configSuccess check: The parsed file prints with no errors.
When to use
You hit this exact error while using docker-compose. The trigger list below matches your situation.
When NOT to use
If the error text differs, or the failure is in a different tool (e.g. a Docker daemon error when you only need the compose fix), find the skill for that exact error instead.
Tool compatibility
docker-compose v1 and Compose v2. Any OS.
Variant phrasings
refers to undefined volume
invalid compose project
Root cause
The service mounts a named volume (a bare name like dbdata:/var/lib/db), but the name is never declared in the top-level volumes: section. Compose treats undeclared named volumes as a project error rather than silently creating an empty volume under a possibly-misspelt name.
Edge cases
- Bind mounts (./data URIs/data, /abs/path:/x) and bare container paths (/data) need no declaration; only bare names do.
- A misspelled name on either side triggers this: the volume gets created under the misspelt name on older versions.
- With extends:, top-level volumes: are NOT inherited; each entry-point file must declare the volume.
Where this came from
Resolved from the public discussion at https://stackoverflow.com/questions/69867166 (so:69867166).
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.