Skip to content

How containers talk to each other

Working knowledgeConcept

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.

Two containers on one bridge network
One user-defined bridge network (addresses from a real run)
NATvethveth
host eth0:8080
bridge172.19.0.1
api172.19.0.2
cache172.19.0.3
  • 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.

Shell
docker network create shop
docker run -d --name api --network shop nginx:alpine
docker network inspect shop --format '{{json .IPAM.Config}}'
Text
[{"Subnet":"172.19.0.0/16","Gateway":"172.19.0.1"}]

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:

Shell
docker run --rm --network shop alpine sh -c 'cat /etc/resolv.conf; nslookup api'
Text
nameserver 127.0.0.11
options ndots:0
…
Name: api
Address: 172.19.0.2

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.

Default bridge
$ docker run -d --name web nginx:alpine
$ docker run --rm alpine wget -qO- web
wget: bad address 'web'
Your own network
$ 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:

A request to a published port
Step 1 of 6
Where the packet has been
Empty
Packet header (example addresses)
IP:port
Source–
Destination–
  • 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.

Ways to write -p
FlagWho can connect
-p 8080:80Anyone who can reach the host on 8080, on every interface
-p 127.0.0.1:8080:80Only programs on the host itself
-p 80Anyone, on a random free host port (see docker port)
EXPOSE 80 in a DockerfileNobody 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.

compose.yaml
services:
api:
image: nginx:alpine
ports:
- "18090:80"
cache:
image: redis:7-alpine
Shell
docker compose -p b25shop up -d
docker network ls --filter name=b25shop
docker compose -p b25shop exec api getent hosts cache
docker compose -p b25shop exec cache redis-cli -h cache ping
Text
b25shop_default bridge
172.19.0.2 cache cache
PONG

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.

From another containercache:6379Service name and the port the program listens on inside its container. No ports: needed.
From your laptoplocalhost:18090Only for published ports, and the host side of the mapping.

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.

07 Check yourself

Question

Compose runs api and db (Postgres, ports: ["15432:5432"]). What should api connect to?

Question

You start two containers with plain docker run and no --network. Why does ping web fail from the other one?

Question

A container runs with -p 8080:80 and the app listens on 127.0.0.1:80 inside it. What does curl localhost:8080 on the host get?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?