When Two Docker Compose Stacks Fight Over the Same Network Name

Roughly half of all requests failed with a Postgres password error that had nothing to do with credentials. A shared external network meant the hostname `db` resolved to two different containers, and the resolver picked one at random per connection.

When Two Docker Compose Stacks Fight Over the Same Network Name

"Password authentication failed for user qasida_user" on roughly half of all requests, with credentials that were never wrong. That's the kind of bug report that sends you straight to the wrong place — the password, the .env file, a stale secret — when the actual fault was a Compose network topology copied from an unrelated deployment.

One name, two Compose stacks, one shared network
One name, two Compose stacks, one shared network

The resolver answers db with both addresses; each connection goes to whichever it happens to pick.

How the name got shared in the first place

The production compose file put Postgres and Redis on a second, external Docker network — modeled on the Ghost deployment sharing the host, and carried over without re-examining what "shared" actually implies. A shared Compose network is a shared DNS namespace. db is about as collision-prone a service name as exists: it's the first thing anyone names their database container. Another project on that same host had its own db service, sharing the same alias on the same network.

What that actually does to DNS resolution

Docker's embedded DNS resolver, asked to resolve db on a network where two containers hold that alias, doesn't error — it answers with both addresses, and each individual connection goes to whichever the client happens to pick. That's the mechanism behind "roughly half" failing: it's not a fixed broken connection, it's a coin flip on every new connection Django opens to the database.

The giveaway, per the commit, was in a single traceback that resolved db to two different addresses — one refusing the connection outright, the other rejecting a role ("qasida_user") it had genuinely never heard of, because it belonged to an entirely different application's schema.

The fix: stop sharing what nothing else needs

# Nothing outside this project needs to reach its database, so nothing
# outside can now see it.

Postgres, Redis, and the Celery containers moved onto the network Compose namespaces automatically per project — where the alias db cannot be claimed by anyone else, because the network itself is scoped to this project. Only web and the media container join a frontend network, because Traefik needs to route to them specifically. The external backend network that caused the collision is gone entirely.

Postgres also gained a healthcheck, with dependents waiting on it rather than on the container merely existing — because the same traceback showed a plain connection-refused error too, which is what talking to a Postgres still working through its own startup sequence looks like, and "started" in Compose terms is not "ready to accept connections."

The false leads that weren't the cause

The commit is careful to rule out two things that had shipped recently and looked suspicious purely by proximity in time:

Neither the logging nor the entrypoint work released before this caused any of it: the 50% failure rate was measured from outside before that deploy went out.

That's a discipline worth naming on its own: when a bug surfaces near a recent deploy, it's tempting to blame whatever just shipped. Here, the failure rate was independently measured before the suspected deploy, which ruled it out cleanly rather than by argument. What that deploy's changes did do was surface a fault that had been silently present for days — the entrypoint's clearer logging (the subject of the next post) was what finally made the failure say what it actually was, in one traceback instead of an ambiguous pattern of intermittent errors.

Series: Qasida App.