Skip to content
LF Cert Trainer

Lesson 107 · Section 9: Networking

Lesson 107: Docker Networking: none, host and bridge

How Docker connects a container to the network: the three built-in networks, the docker0 bridge and its veth pairs, NAT for outgoing traffic and published ports for incoming traffic. The single-host picture that Kubernetes networking grows out of. · 16 min read · 4 min video

1/10Docker Networking
0:00 / 3:58

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

What this lesson is about

The previous lesson built a small network by hand: network namespaces, veth pairs and a Linux bridge. Docker does exactly that work for you every time you run a container. Seeing how it does it gives you a complete, working example of container networking on one host, and it shows clearly which part breaks when you add a second host. That broken part is the reason Kubernetes has its own network model and the reason CNI exists.

This lesson answers three questions:

  • Which networks can a container be attached to, and what does each give it?
  • How does the default bridge network carry a packet out of a container and back?
  • What does -p 8080:80 really do?

The three built-in networks

Docker creates three networks at installation. You choose one with --network on docker run:

docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
3a1f0c9d2b7e   bridge    bridge    local
9c44e1b0a5d2   host      host      local
51d7f3e8c6a0   none      null      local
NetworkThe container getsReachable from outsideTypical use
noneA namespace with only loNoBatch jobs that must not use the network
hostNo namespace of its own: the host's interfacesYes, on the host's address and the port the program opensNode tools, maximum performance
bridge (default)A namespace, an eth0 with a private address, a default routeOnly through published portsAlmost everything
Three ways to attach a containersharesvethrouted, then NATLAN--network noneloopback only--network hostno own namespaceDefault: bridge172.17.0.2Host networketh0 192.168.1.10docker0 bridge172.17.0.1/16Other machines192.168.1.0/24
Three ways to attach a container. none gives the container a namespace with nothing in it, host gives it no namespace of its own, and bridge gives it a private address behind the docker0 bridge.

Parts:

  • --network none (loopback only)
  • --network host (no own namespace)
  • Default: bridge (172.17.0.2)
  • Host network (eth0 192.168.1.10)
  • docker0 bridge (172.17.0.1/16)
  • Other machines (192.168.1.0/24)

Connections:

  • --network host to Host network: shares
  • Default: bridge to docker0 bridge: veth
  • docker0 bridge to Host network: routed, then NAT
  • Host network to Other machines: LAN

none

docker run --rm --network none alpine:3.22 ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128

The container has a network namespace, but Docker put nothing in it. It cannot reach other containers, the host or the internet, and nothing can reach it.

host

docker run -d --name web-host --network host nginx:1.29-alpine
curl -sI http://localhost:80 | head -n 1
HTTP/1.1 200 OK

With --network host Docker does not create a network namespace. The process sees the host's interfaces and opens port 80 of the host. There is no isolation and no address translation, which has two consequences:

  • Two containers on the host network cannot both listen on the same port. The second one fails with Address already in use.
  • -p has no meaning. Docker prints WARNING: Published ports are discarded when using host network mode.

bridge

Without --network, a container joins the network called bridge. The rest of this lesson is about that network, because it is the model Kubernetes nodes follow.

How the bridge network works

When Docker is installed it creates a Linux bridge called docker0 on the host and gives it the address 172.17.0.1/16. A bridge is a virtual switch. Because it also has an IP address, it is at the same time an interface of the host, and the host can route between it and its other interfaces.

For every container on the bridge network, Docker does the steps you did by hand in the Network Namespaces lesson:

  1. Create a network namespace
  2. Create a veth pair
  3. Attach one end to docker0
  4. Move the other end inside as eth0
  5. Assign the next free address
  6. Add a default route via 172.17.0.1
What docker run does for the network
Inside the default bridge networkDocker hostveth pairveth pairforwardmasqueradeContainer webeth0 172.17.0.2Container dbeth0 172.17.0.3docker0172.17.0.1/16Host eth0192.168.1.10Other machinessee 192.168.1.10
Inside the default bridge network. Each container has its own network namespace. A veth pair joins it to docker0, which is a virtual switch on the host and also the containers' gateway.

Parts:

  • Container web (eth0 172.17.0.2), in Docker host
  • Container db (eth0 172.17.0.3), in Docker host
  • docker0 (172.17.0.1/16), in Docker host
  • Host eth0 (192.168.1.10), in Docker host
  • Other machines (see 192.168.1.10)

Connections:

  • Container web to docker0: veth pair
  • Container db to docker0: veth pair
  • docker0 to Host eth0: forward
  • Host eth0 to Other machines: masquerade

Every part is visible with ordinary Linux tools. On the host:

ip -br addr show docker0
ip -br link show master docker0        # the host ends of the veth pairs
docker0          UP             172.17.0.1/16
veth4c1e9a2@if2  UP             ae:5b:10:3f:77:c2 <BROADCAST,MULTICAST,UP,LOWER_UP>
veth90b7d15@if2  UP             52:c8:0a:6e:1d:9f <BROADCAST,MULTICAST,UP,LOWER_UP>

Inside a container:

docker exec web ip -br addr
docker exec web ip route
lo               UNKNOWN        127.0.0.1/8
eth0@if7         UP             172.17.0.2/16

default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 scope link  src 172.17.0.2

The two ends of a veth pair name each other by interface index. eth0@if7 in the container means "my peer is interface number 7 on the other side", and on the host ip link lists number 7 as veth4c1e9a2@if2.

Container to container

Two containers on the same bridge are on the same virtual switch, so they reach each other directly by address:

docker run -d --name db redis:8-alpine
docker exec web ping -c 1 172.17.0.3

On the default bridge they cannot use names: ping db fails with bad address 'db'. Name resolution arrives with user-defined networks, described below.

Container to the outside

A packet for the internet leaves the container through its default route and arrives on docker0. From there the host acts as a router. Two things make that work:

  • IP forwarding must be on (net.ipv4.ip_forward = 1). Docker switches it on.
  • The source address 172.17.0.2 means nothing outside the host, so no reply could ever come back. Docker adds a masquerade rule: packets from the bridge range that leave through another interface get the host's address as their source.
sudo iptables -t nat -S POSTROUTING
-P POSTROUTING ACCEPT
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE

Other machines only ever see the host's address, 192.168.1.10. This is source NAT, the same thing a home router does for the devices behind it.

The outside to a container: published ports

The reverse direction does not work by itself. 172.17.0.2 is not routable from other machines, and even if it were, the address changes whenever the container is recreated. To let traffic in, you publish a port:

docker run -d --name shop -p 8080:80 nginx:1.29-alpine

-p 8080:80 reads "host port 8080 goes to container port 80". Docker implements it with a destination NAT (DNAT) rule in its own chain of the nat table:

sudo iptables -t nat -S DOCKER
-N DOCKER
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.3:80
A published port, packet by packetcurlmatches --dport 8080to :80Client192.168.1.50Host port 8080192.168.1.10:8080DNAT rulenat table, DOCKERContainer web172.17.0.2:80
A published port, packet by packet. -p 8080:80 adds a destination NAT rule. A packet for port 8080 of the host is rewritten to port 80 of the container and forwarded over docker0.

Parts:

  • Client (192.168.1.50)
  • Host port 8080 (192.168.1.10:8080)
  • DNAT rule (nat table, DOCKER)
  • Container web (172.17.0.2:80)

Connections:

  • Client to Host port 8080: curl
  • Host port 8080 to DNAT rule: matches --dport 8080
  • DNAT rule to Container web: to :80

A packet that arrives on the host for TCP port 8080 has its destination rewritten to 172.17.0.3:80 and is forwarded over the bridge. The reply is translated back on the way out.

FormMeaning
-p 8080:80Host port 8080, all host addresses, to container port 80 (TCP)
-p 127.0.0.1:8080:80Only on the host's loopback address
-p 8080:80/udpUDP instead of TCP
-p 80Container port 80 on a random free host port
-PEvery port the image declares with EXPOSE, each on a random host port
Under the hood · User-defined bridges, the built-in DNS and other drivers

The default bridge is kept simple for backward compatibility. A bridge network that you create yourself behaves better:

docker network create --subnet 10.10.0.0/24 shopnet
docker run -d --name api --network shopnet nginx:1.29-alpine
docker run --rm --network shopnet alpine:3.22 ping -c 1 api
  • Docker creates a new bridge on the host (named br-<network id>) with its own subnet. Containers on different bridges cannot reach each other, so a network is also an isolation boundary.
  • Containers on a user-defined network resolve each other by name. Docker runs a small DNS server that every such container reaches at 127.0.0.11 (look at /etc/resolv.conf inside one).
  • A container can be attached to several networks: docker network connect shopnet web.

Docker has more drivers than the three above. overlay connects containers across hosts in Swarm mode, and macvlan and ipvlan put containers directly on the physical network. They are Docker's own answers to the multi-host problem. Kubernetes does not use them.

Two details about published ports: Docker also starts a small docker-proxy process per published port, which is why ss -tlnp shows the port as listening on the host, and recent Docker versions can program nftables instead of iptables. The idea, a DNAT rule in front of the bridge, is the same.

From Docker to Kubernetes

Everything above works on one host. Add a second host and it falls apart:

  • Both hosts use 172.17.0.0/16, so two containers on different hosts can have the same address.
  • A container on host A has no route to a bridge address on host B.
  • The only way across is a published port, so every program must know host addresses and remapped port numbers, and two programs cannot both have host port 80.

Kubernetes was designed to avoid exactly this. Its network model demands that every Pod has an address that is unique in the whole cluster and that all Pods reach each other directly, without NAT and without port mapping.

Docker on one hostKubernetes
One namespace per containerOne namespace per Pod, shared by its containers
docker0, same range on every hostA bridge or similar per node, with a different Pod range per node
Containers reach other hosts through -pPods reach Pods on any node by Pod IP
Docker itself wires the networkThe container runtime calls a CNI plugin
-p 8080:80 for incoming trafficServices (NodePort, LoadBalancer), Ingress or Gateway

One Docker feature maps directly to a Pod. --network container:<name> starts a container inside the network namespace of another container:

docker run -d --name base nginx:1.29-alpine
docker run --rm --network container:base alpine:3.22 wget -qO- http://localhost:80 | head -n 4

The second container reaches nginx on localhost because both share one namespace, one address and one set of ports. That is how the containers of a Pod are joined: a tiny pause container owns the namespace, and every other container of the Pod is attached to it.

Using it

Example · Trace a container's interface to its veth on the host

A host has many veth interfaces. Find the one that belongs to the container web.

docker exec web cat /sys/class/net/eth0/iflink      # index of the peer on the host
7
ip -o link | grep '^7:'
7: veth4c1e9a2@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP ...

Interface 7 is the host end, and master docker0 confirms that it is plugged into the bridge. The same method works on a Kubernetes node with kubectl exec in place of docker exec.

Example · Use the host's tools inside a container that has none

Many images have no ip, ss or ping. Enter only the network namespace of the container and keep the host's file system, so the host's tools are available:

PID=$(docker inspect -f '{{.State.Pid}}' web)
sudo nsenter -t "$PID" -n ip -br addr
sudo nsenter -t "$PID" -n ss -tlnp
lo               UNKNOWN        127.0.0.1/8
eth0@if7         UP             172.17.0.2/16

State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port  Process
LISTEN  0       511           0.0.0.0:80         0.0.0.0:*      users:(("nginx",pid=4312,fd=6))

-t names the process whose namespaces to enter and -n picks the network namespace only.

Example · Publish a port and prove each hop

docker run -d --name shop -p 8080:80 nginx:1.29-alpine
docker port shop
80/tcp -> 0.0.0.0:8080
80/tcp -> [::]:8080

Check the three places where it can break, from the inside out:

docker exec shop wget -qO- http://localhost:80 >/dev/null && echo app-ok      # 1. the program listens
curl -s -o /dev/null -w '%{http_code}\n' http://172.17.0.3:80               # 2. the bridge reaches it
curl -s -o /dev/null -w '%{http_code}\n' http://192.168.1.10:8080           # 3. the published port works
sudo iptables -t nat -S DOCKER | grep 8080                                  # the rule behind step 3
app-ok
200
200
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.3:80

If step 1 and 2 pass and step 3 fails, the mapping is wrong (for example -p 8080:8080 for a program that listens on 80) or a firewall on the host blocks the port.

Example · See that one bridge range cannot span two hosts

On two Docker hosts, run the same command:

docker run --rm alpine:3.22 hostname -i
172.17.0.2

Both print 172.17.0.2. Two containers with the same address on two hosts cannot talk to each other by that address, and neither host has a route to the other's bridge. Keep this picture in mind: the Pod Networking lesson solves it by giving every node its own range and adding routes between the nodes.

Tricks

Common mistakes

  • Expecting a bridge address to work from another machine. 172.17.0.x exists only inside one host. Use a published port and the host's address.
  • Reversing the port mapping. -p 80:8080 publishes host port 80 to container port 8080. The left side is the host.
  • Thinking EXPOSE or --expose opens a port. They only document it. Only -p, -P or the host network make a container reachable from outside.
  • Using -p together with --network host. The mapping is discarded. The program is reachable on whatever port it opens on the host.
  • Using container names on the default bridge. Names resolve only on user-defined networks.
  • Looking for containers with ip netns. Docker's namespaces are not under /run/netns. Use docker inspect and nsenter.
  • Carrying the Docker picture to Kubernetes unchanged. Pods do not use port mapping to reach each other, and Pod addresses are valid across the whole cluster.

Recap

  • Docker has three built-in networks: none (loopback only), host (the host's own network) and bridge (the default).
  • On the bridge network each container gets a namespace, a veth pair to docker0 and an address from 172.17.0.0/16, with 172.17.0.1 as its gateway.
  • Outgoing traffic is forwarded by the host and masqueraded behind the host's address.
  • Incoming traffic needs a published port: -p HOST:CONTAINER adds a DNAT rule in the nat table.
  • The bridge range is local to one host, so this model does not connect containers across hosts.
  • Kubernetes keeps the namespace, veth and bridge, requires cluster-wide Pod addresses without NAT, and lets a CNI plugin do the wiring.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.