Where a container's network ends

A container has its own network on a private street inside the host. Docker joins that street to the outside with two rewrite rules, one for traffic that leaves and one for each published port. The second rule is applied before the host's firewall rules for incoming traffic.

Part 7 of 9 · Networking3 min read
your firewallin: a DNAT rule8080 becomes 172.17.0.2:80-p 8080:80web = .2names: Docker's own DNSonly on networks you createMASQUERADEinternetout: MASQUERADEthe rewrite from part 6iptables -t nat -SDocker writes its own rulesin front of your firewall

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:

Shell
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. 1docker network inspect bridge --format '...' prints one part of the settings of the network bridge.
  2. 2--net=host makes the container use the network of the host itself.
  3. 3iptables -t nat -S POSTROUTING lists the NAT rules for packets that leave. grep 172.17.0.0 keeps 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?

Shell
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. 1docker network create demo makes a network, and --network demo puts a container on it.
  2. 2nslookup web asks the resolver for the name web.
  3. 3grep -A1 ^Name prints the line that starts with Name and 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:

Shell
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. 1wget -q -T 2 -O- http://plain downloads 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.

default bridgeplain?192.168.65.7the host resolverbad addressyour own networkweb?127.0.0.11Docker's resolver172.21.0.2
On a network you create, Docker answers name questions. On the default bridge, the question goes to the host's resolver, which does not know the 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:

Shell
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. 1-p 8080:80 publishes the container's port 80 as port 8080 on the host.
  2. 2iptables -t nat -S DOCKER lists the NAT rules that Docker writes.
  3. 3grep -- "--dport 8080" keeps the rule for port 8080. The -- tells grep that 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.

arrivesto host:8080the host gets ithostcontainerrewrittento 172.17.0.2:80DNAT: a new destinationhostcontaineranswerfrom host:8080rewritten backhostcontainer

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 MASQUERADE rule, 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.
  • -p writes a DNAT rule 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.1 when only the host needs it.

Next, in part 8: one front door for many programs.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS