Sub topic 1
How Docker Isolates Container Networking
Prerequisites
This chapter assumes you can run containers with docker run and use a Linux-style shell. You should know what an IP address, a subnet, a port, and DNS are. You don't need any Docker networking knowledge yet.
The idea: one network stack per container
Every container gets its own network stack. A network stack is the set of interfaces, IP addresses, routing table, firewall rules, and port numbers that a process sees. Because each container has a separate stack, two containers can both listen on port 80 without conflict, and a process in one container cannot see the network interfaces of another.
Docker does not implement this itself. It uses two Linux kernel features.
- A network namespace is an isolated copy of the network stack. Processes inside a namespace see only the interfaces that were placed in it. Docker creates one namespace for each container (unless you ask for something else, such as the host driver covered later).
- A virtual Ethernet pair, or veth pair, is a virtual cable with two ends. Whatever enters one end comes out of the other. Docker puts one end inside the container's namespace and leaves the other end in the host's namespace.
How the pieces connect
On the default setup, the host end of each veth pair is plugged into a virtual switch called a Linux bridge. The bridge is named docker0 by default. Containers attached to the same bridge can exchange traffic through it. The host also holds an IP address on the bridge, which acts as the containers' gateway to the outside.
Inside each container, the interface appears as eth0. On the host, its partner has a name such as veth3f2a1c9.
Seeing the isolation yourself
Start a container and list its interfaces. Then list the host's interfaces and compare.
docker run -d --name demo nginx:alpine
docker exec demo ip addr show eth0
ip link show type vethThe first command shows the container's eth0 and its IP address. The second shows the veth ends that exist in the host's namespace. The container's address belongs to a different subnet than your LAN, and the container cannot see your host's physical interfaces.
You can also compare the namespaces directly. Each process has a namespace identifier under /proc:
PID=$(docker inspect --format '{{.State.Pid}}' demo)
sudo readlink /proc/$PID/ns/net
readlink /proc/self/ns/netThe two printed identifiers differ. That difference is the isolation.
Clean up when you are done:
docker rm -f demoWhat isolation does and does not give you
Network namespaces separate stacks. They are not a security boundary on their own, because containers on the same bridge can still reach each other. Later chapters show how to control that with separate networks.
| Property | Inside a container | On the host |
|---|---|---|
| Interfaces | `lo` and `eth0` | Physical interfaces, bridges, and veth ends |
| IP address | Private address from the bridge subnet | LAN address plus the bridge address |
| Port numbers | Own set, so port 80 can be reused | Shared by all host processes |
| Routing table | Default route through the bridge gateway | Default route to the LAN router |
Where this leads
The bridge is only one way to wire a namespace to the world. Docker calls each wiring method a network driver. The next chapter compares the built-in drivers and when to pick each one.
Exercise
Two containers both run a web server listening on port 80, and neither fails with "address already in use". What two kernel features explain this, and what does each do?
Show answer
Network namespaces give each container its own network stack, so each has its own set of port numbers and port 80 is free in both. Veth pairs act as virtual cables that connect each container's namespace to the host's namespace, usually to a bridge, so the containers can still communicate.
Exercise
How can you show that a running container's network namespace differs from the host's?
Show answer
Get the container's process ID with docker inspect --format '{{.State.Pid}}' NAME, then compare sudo readlink /proc/PID/ns/net with readlink /proc/self/ns/net. The identifiers are different.