In the Containers series, a container got its own view of the network: its own interface, address and routes. Parts 1 to 6 explain the pieces of that view. Docker builds them for you.
In the post office picture, each container is a house on a private street. The gate at the end of the street is the NAT rule from part 6: it rewrites return addresses. In this part we look at the street, the names on it, and a second gate for visitors, which -p adds.
The street is a bridge
Docker creates a virtual switch on the host, a bridge, and gives it an address range. Every container on it is a house with its own address. Ask Docker for the range. Then ask the host for the rule that connects the street to the outside:
docker network inspect bridge --format '{{(index .IPAM.Config 0).Subnet}} gateway {{(index .IPAM.Config 0).Gateway}}'
docker run --rm --net=host --cap-add NET_ADMIN alpine sh -c '
apk add -q iptables && iptables -t nat -S POSTROUTING | grep 172.17.0.0' 172.17.0.0/16 gateway 172.17.0.1 -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
Here is what each marked part does:
- 1
docker network inspect bridge --format '...'prints one part of the settings of the networkbridge. - 2
--net=hostmakes the container use the network of the host itself. - 3
iptables -t nat -S POSTROUTINGlists the NAT rules for packets that leave.grep 172.17.0.0keeps the rule for our street.
The street is 172.17.0.0/16, and its gateway, 172.17.0.1, is the bridge on the host. The second line is the rule from part 6. Everything from the street that leaves through another interface gets the host’s address as its return address. With --net=host, the command looks at the host’s own network. On Docker Desktop, the host is the small Linux machine that runs Docker.
Names only work on your own streets
In part 2, a resolver turned names into addresses. Docker runs one for you, but not on every street. We start a web server on a network that we create, and look it up by name from a second container:
Will the same lookup work on the default bridge?
docker network create demo
docker run -d --name web --network demo nginx:alpine
docker run --rm --network demo alpine sh -c '
grep nameserver /etc/resolv.conf
nslookup web | grep -A1 ^Name' nameserver 127.0.0.11 Name: web Address: 172.21.0.2
Here is what each marked part does:
- 1
docker network create demomakes a network, and--network demoputs a container on it. - 2
nslookup webasks the resolver for the nameweb. - 3
grep -A1 ^Nameprints the line that starts withNameand the line after it.
The resolver at 127.0.0.11 belongs to Docker. It answers with the address of the container named web. Compose from the Containers series creates such a network for each project by default, so one service can call another by its name. The default bridge has no such resolver:
docker run -d --name plain nginx:alpine
docker run --rm alpine sh -c '
grep nameserver /etc/resolv.conf
wget -q -T 2 -O- http://plain' nameserver 192.168.65.7 wget: bad address 'plain'
Here is what each marked part does:
- 1
wget -q -T 2 -O- http://plaindownloads the page and prints it (-O-). It gives up after 2 seconds (-T 2).
Here the container uses the resolver of the host, which knows nothing about plain. The address 192.168.65.7 is the resolver of Docker Desktop. On Linux, Docker copies the resolver of the host, so your address will differ. Use your own network whenever containers must find each other by name.
A published port is a gate with a rewrite rule
The address of a container is private, so a machine outside the host has no route to it. The flag -p adds a second rewrite rule, in the opposite direction. Publish a web server on port 8080 and read the rule:
docker run -d --name pub -p 8080:80 nginx:alpine
docker run --rm --net=host --cap-add NET_ADMIN alpine sh -c '
apk add -q iptables && iptables -t nat -S DOCKER | grep -- "--dport 8080"' -A DOCKER -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.2:80
Here is what each marked part does:
- 1
-p 8080:80publishes the container's port 80 as port 8080 on the host. - 2
iptables -t nat -S DOCKERlists the NAT rules that Docker writes. - 3
grep -- "--dport 8080"keeps the rule for port 8080. The--tellsgrepthat its own options end there.
DNAT is the mirror of the MASQUERADE rule. A packet for port 8080 on the host gets a new destination, 172.17.0.2:80, the container. The address 172.17.0.2 depends on which addresses are already in use, so yours can differ. The router remembers the pair, as in part 6, and rewrites the reply back.
A packet arrives at the host on port 8080.
Clean up with docker rm -f web plain pub and docker network rm demo.
Key points
- A container lives on a private street, a bridge with its own address range, and the bridge is its gateway.
- Traffic that leaves the street is rewritten by a
MASQUERADErule, the same NAT as in part 6. - Docker answers name questions only on networks you create. On the default bridge, containers cannot find each other by name.
-pwrites aDNATrule that sends a port of the host to a port of the container.- Packets for a published port are forwarded, so the input rules of the host firewall do not see them. Bind the port to
127.0.0.1when only the host needs it.
Next, in part 8: one front door for many programs.