🐳 Fix a Compose Stack Silently Reusing a Stale Anonymous Volume Because a Service Was Renamed Without Renaming Its Volume
A volume declared under Compose’s top-level volumes: section gets its actual Docker-level name from the volume’s own key (or an explicit name: field) — never from whichever service happens to mount it. Rename a service in docker-compose.yml as part of a refactor, while leaving the volume key underneath it untouched, and Compose has no reason to treat it as a fresh volume: the exact same named volume, with all of its existing data, gets mounted into the renamed service exactly as before. For a team expecting a service rename (especially one paired with a major image version bump) to mean a clean slate, this quietly resurfaces the previous service’s entire dataset instead.
🔎 The Problem
# v1 - service named "postgres"
services:
postgres:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
# v2 - service renamed to "db" for clarity, volume key left unchanged
services:
db:
image: postgres:17 # new major version, expecting a clean slate
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: # <- same key, Docker still resolves this to myproject_pgdata
# docker volume ls
# myproject_pgdata <- still present, still full of the OLD service's data
✅ Fix: Rename the Volume Too, or Explicitly Drop It, When You Actually Want a Clean Slate
- Remember that a Compose volume’s real Docker identity comes from its key under the top-level
volumes:section (or an explicitname:field), never from whichever service happens to mount it — renaming a service’s key does nothing to the volume sitting underneath it. - If the rename is meant to represent a genuinely fresh deployment, rename the volume key (or add an explicit
name:) at the same time, or remove the old volume withdocker volume rmafter confirming you don’t still need its data, rather than assuming a new service key implies a new volume. - Run
docker compose down -vonly when you explicitly want to discard the named volume along with the containers — a plaindocker compose downintentionally leaves named volumes in place by design, which is exactly how the “stale” data on a renamed service survives.
🚨 Why This Is Dangerous
- A service rename done as part of a blue/green cutover or a schema-breaking migration can silently inherit the old service’s entire dataset instead of starting clean, so what looked like an isolated new deployment is actually still reading from and writing to the exact same disk as the thing it was meant to replace.
In Compose, a volume belongs to its name, not to whichever service happens to be mounting it this week — rename the service all you want, the data doesn’t even notice.

