DockerMid
Container can't reach another container by name
Containers on the default bridge network can only reach each other by IP. Put them on a user-defined bridge network so names resolve automatically.
What you see
ping: bad address 'db'orcould not resolve host: dbfrom inside a container- Connecting by IP address works, connecting by name does not
What it means
Docker's default bridge network does not provide name resolution between containers. The docs say containers on the default bridge can only access each other by IP address, unless you use the legacy --link option.
On a user-defined bridge network, containers resolve each other by name or alias. This is the usual fix.
How to confirm the cause
- 1See which network each container is on
docker inspect --format '{{json .NetworkSettings.Networks}}' CONTAINERIf both show only
bridge, name resolution will not work between them. - 2List networks
docker network ls
- 3Test resolution from inside
docker exec CONTAINER getent hosts OTHER_NAME
No output means the name does not resolve on that network.
Causes and fixes
Both containers are on the default bridge
- HOW TO TELL
- Both show only the
bridgenetwork. - ROOT CAUSE
- The default bridge is a legacy network without automatic DNS between containers. It also puts unrelated containers on one shared network, which the docs list as a risk.
- FIX
- Create a user-defined bridge and attach both containers to it.
docker network create app-net docker run -d --name db --network app-net postgres:18 docker run -d --name web --network app-net IMAGE
The containers are on different networks
- HOW TO TELL
- They share no network in
docker inspect. - ROOT CAUSE
- Name resolution only works inside one network. Containers on separate networks cannot see each other.
- FIX
- Attach one of them to the other's network.
docker network connect app-net CONTAINER
How to stop it coming back
- Always create a named network for a multi-container app instead of using the default bridge.
- Refer to services by name, never by IP address.
Keep going
- Runbook: Compose service starts before its database is ready
- Runbook: Local DNS resolver found in resolv.conf and containers can't use it
- Cheat sheet: Docker
- Cheat sheet: Docker Compose
- Lesson: the networking topic in Learn
Sources
Docker documentation (docs.docker.com), read 18 Nov 2026 from the official docs repository.