How containers talk to each other
Your app works on your laptop, then fails in a container because localhost no longer finds the database. Here is the network Docker builds between containers, how names and published ports work on it, and which address to use from where.
01 localhost is a different place in a container
You move your app into a container, start Postgres in another, and the app fails with ECONNREFUSED 127.0.0.1:5432. Postgres is running and you can reach it from your laptop. So what is the app talking to?
Itself. Each container gets its own network namespace: its own interfaces, its own routing table, and its own localhost. Inside the app’s container, 127.0.0.1 is the app’s container and nothing else. Postgres is listening on a different localhost, one the app cannot see.
Containers reach each other the way separate machines do, over a network. Docker builds that network for you; the rest of this page is what it builds and how to address it. Outputs below are real runs on Docker 29.8 (Docker Desktop on macOS, which runs containers in a small Linux VM).
02 The picture: a switch with a cable per container
By default Docker connects containers with a bridge network. Create one, and Docker makes a virtual switch in the kernel and gives it a private subnet. Each container you attach gets a virtual cable to the switch and an address on that subnet.
- the bridge (gateway)
The bridge takes the first address as the gateway. Containers talk to each other directly through it; traffic to and from the outside is translated (NAT) at the host.
A second container on shop got 172.19.0.3, as ip addr inside it shows. The subnet Docker picks depends on which ranges are already in use on your machine, so yours may differ.
03 Reaching another container by name
Addresses change every time a container is recreated, so you should not hard-code them. On a network you created, Docker runs a small DNS server that answers with container names. Every container’s /etc/resolv.conf points at it:
127.0.0.11 is Docker’s embedded DNS. It knows the names of the containers on the same network, and passes anything else on to the host’s DNS. There is one catch: the default network, called bridge, that containers join when you give no --network, has no such DNS.
$ docker run -d --name web nginx:alpine$ docker run --rm alpine wget -qO- webwget: bad address 'web'
$ docker run -d --name web --network shop nginx:alpine$ docker run --rm --network shop alpine wget -qO- web…<title>Welcome to nginx!</title>…
On the default bridge the containers can still reach each other by IP address (the second container fetched the page from 172.17.0.5 directly). Names only work on networks you create, which is one reason to always create one.
04 Publishing a port to the host
Nothing outside the bridge can reach a container’s port until you publish it with -p host:container. Publishing tells Docker to forward a host port to the container’s port. Follow one request in:
- just happened
- just rewritten
- delivered
- no packet yet
The api container was started with -p 8080:80. nginx listens on port 80 inside it, at 172.19.0.2. A browser on another machine is about to request http://203.0.113.10:8080, the host’s address.
| Flag | Who can connect |
|---|---|
| -p 8080:80 | Anyone who can reach the host on 8080, on every interface |
| -p 127.0.0.1:8080:80 | Only programs on the host itself |
| -p 80 | Anyone, on a random free host port (see docker port) |
| EXPOSE 80 in a Dockerfile | Nobody new: it documents the port but publishes nothing |
On Docker Desktop there is a VM between your Mac and the containers, and Docker Desktop forwards the published port from the Mac into the VM. The outcome is the same: curl localhost:18080 on the Mac returned the nginx page for a container started with -p 18080:80.
05 Compose sets this up for you
docker compose up creates a network for the project, attaches every service to it, and registers each service name in the embedded DNS. So services find each other by the names in the file.
The network is named <project>_default. api reaches Redis at cache:6379, its container port, even though cache publishes nothing. Only api is reachable from the host, at localhost:18090.
ports: needed.06 When it bites
DATABASE_HOST=localhostlocalhost points back at the containerUse the other container’s service name, such as db. To reach a program on the host itself, Docker Desktop provides host.docker.internal; on Linux add --add-host=host.docker.internal:host-gateway.--bind 127.0.0.1The app listens only on the container’s loopbackForwarded traffic arrives on the container’s eth0, not its lo. A Python http.server bound to 127.0.0.1 behind -p 18081:8000 gave curl: (56) Recv failure: Connection reset by peer; bound to 0.0.0.0, it returned 200.api:8080Using the published port between containersInside the network, the host side of -p 8080:80 does not exist. Connecting to api:8080 got Connection refused; use api:80.--name webNames do not resolve on the default bridgeContainers started without --network share the default bridge network, which has no DNS for names. Create a network, or use Compose.-p 5432:5432A published port is open to the worldWithout an address, Docker publishes on every interface. On Linux its NAT rules are applied before host firewalls such as ufw see the traffic, so a database can be exposed by accident. Publish to 127.0.0.1 when only the host needs it.EXPOSE in a Dockerfile does not open anything. Only -p (or ports: in Compose) makes a container port reachable from outside its network.