🐳 Docker: Fix a Container’s DNS Resolving a Name It Already Left
Docker’s embedded DNS resolver at 127.0.0.11 serves service-name lookups inside a container, but most application runtimes cache a resolved IP address for the life of the process rather than re-resolving on every connection — after a docker network disconnect/connect changes which network a container actually belongs to, the running process inside it can keep using an address it resolved before the change, even though docker network inspect correctly shows the container’s new, updated network membership.
🔎 The Problem
$ docker network inspect app-net --format '{{.Containers}}'
(api container no longer listed - correctly disconnected)
Inside the api container's process (already running, never restarted):
still connecting to cached IP 172.19.0.5 for 'db'
-> that address now belongs to a different container on the old network
-> connection refused / wrong service responds, looks like 'db' is down
✅ Fix: Treat a Network Change as a Reason to Recreate, Not Just Reconnect
- Treat
disconnect/connecton a running container as something that needs a DNS-cache-aware restart of the process inside it, not just a network-level change — most app runtimes cache a resolved IP for the life of the process. - Resolve the name manually from inside the container immediately after a network change to confirm what it actually returns, rather than assuming the application layer will notice on its own.
- When possible, recreate the container instead of hot-reconnecting its networks, so the one new container that starts gets a fresh resolver state from the beginning.
🚨 Why This Is Dangerous
- A connection that silently keeps going to the right hostname but the wrong actual address after a network topology change can fail in ways that look like the remote service is down, when the real problem is the resolving container’s own stale state.
docker network inspect tells you the truth about right now — it says nothing about what a process already running inside that container cached ten minutes ago.




