Virtualization & Container Networking
Bridges, veth pairs, namespaces, and how Docker and Ansible use them
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.
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.
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.
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.
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.
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
- 1Private host 192.168.1.10:54321 sends packet to 8.8.8.8:443
- 2Router rewrites src IP to public 203.0.113.5:62001 (PAT entry stored)
- 3Reply arrives at 203.0.113.5:62001; router looks up table
- 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.
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.
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.