NetwNETW
Curriculum
CHAPTER 09

Virtualization & Container Networking

Bridges, veth pairs, namespaces, and how Docker and Ansible use them

12 min read8 sections
In this chapter
Why virtual networking existsBridgesveth pairsNetwork namespacesNAT for containersPort mappingOverlay networksAnsible and bridges

01Why virtual networking exists

Virtual machines and containers need network interfaces, but you cannot plug a cable into a VM. The OS creates virtual interfaces and connects them with software. The same concepts (switches, routers, NAT) reappear, just in software.

Why this exists

Understanding virtual networking makes Docker, Kubernetes, and Ansible far less mysterious. Every container network is just a bridge, a veth pair, and a namespace.

02Bridges

A bridge is a software switch running in the kernel. It learns MAC addresses and forwards frames between its ports, exactly like a hardware switch. Docker creates one called docker0 and attaches each container to it.

Mental model

Think of a bridge as a power strip for network cables: each container plugs into a socket, and the strip forwards frames between them.

Bridging and container networking

Watch a frame travel between two containers on the same bridge, then leave the host.

Loading the bridge scene…
A bridge is a software switch. Docker creates one (docker0) and connects each container with a veth pair, a virtual cable.

Bridge: a software switch running in the kernel. It learns MAC addresses and forwards frames between its ports, exactly like a hardware switch.

veth pair: a virtual Ethernet cable with two ends. One end sits inside the container’s network namespace, the other is plugged into the bridge. Whatever goes in one end comes out the other.

Network namespace: an isolated network stack. Each container gets its own, so it thinks it has its own interfaces, routes and firewall.

NAT: when a container talks to the outside world, the host rewrites the source address so replies find their way back, just like a home router.

Docker default bridge: creates docker0, attaches each container via a veth pair, and enables NAT for outbound traffic. Containers on the same bridge can talk to each other directly.

Ansible: often manages bridges and veth pairs on hypervisors to connect VMs to the right network segment.

03veth pairs

A veth pair is a virtual Ethernet cable with two ends. One end sits inside the container network namespace, the other is plugged into the bridge. Whatever goes in one end comes out the other. This is how a container gets an interface that looks like a real NIC.

Why this exists

Without veth pairs, a container would have no way to reach the host network. The veth pair is the virtual cable that connects the container isolated world to the host.

04Network namespaces

A namespace is an isolated network stack. Each container gets its own, so it thinks it has its own interfaces, routes, and firewall. The host keeps its own namespace separate. This is the isolation that makes containers feel like separate machines.

Mental model

Like having separate apartments in one building: each has its own door, plumbing, and address, but they share the same structure.

05NAT for containers

When a container talks to the outside world, the host rewrites the source address so replies find their way back, just like a home router. This is why containers can reach the internet even though they use private IPs.

NAT / PAT Translation

  1. 1Private host 192.168.1.10:54321 sends packet to 8.8.8.8:443
  2. 2Router rewrites src IP to public 203.0.113.5:62001 (PAT entry stored)
  3. 3Reply arrives at 203.0.113.5:62001; router looks up table
  4. 4Router rewrites dst back to 192.168.1.10:54321 and forwards

06Port mapping

Docker maps a host port to a container port (for example, host 8080 to container 80). The host listens on 8080 and forwards traffic to the container. This is how you reach a service inside a container from outside.

Think: Why not give every container a public IP? Because IPv4 addresses are scarce and containers are temporary. Port mapping reuses one public IP.

07Overlay networks

For multi-host container networking, an overlay network wraps container traffic in VXLAN and sends it over the physical network. Each host has a bridge for its containers, and VXLAN tunnels connect the bridges across hosts.

Mental model

Like a private tunnel between two office buildings: each building has its own LAN, and the tunnel lets devices in different buildings talk as if they were on the same LAN.

08Ansible and bridges

Ansible often manages bridges and veth pairs on hypervisors to connect VMs to the right network segment. The same concepts apply: a bridge is a switch, a veth pair is a cable, and a namespace is isolation.

Check your understanding
0 / 2 correct
What is a veth pair?
What does a Linux bridge do?