Container networking: bridges, namespaces and Pods
See how Linux namespaces and veth pairs connect containers, how Docker networks provide isolation, and where Kubernetes CNI fits.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: a user-defined Docker bridge network, an Nginx container, and an Alpine client ran briefly.
getent hosts resolved the network alias, ip showed the client's interface and route, and an HTTP response
came from the container. The script removes its container and network on exit.
Cluster lab: none. Kubernetes networking is explained with a diagram; no cluster was created.
Before containers, each application needed its own machine or a carefully managed shared host. Containers made processes lighter, but those processes still needed separate addresses and a path to other services. Linux namespaces isolate network state; virtual Ethernet links connect that state to a host bridge. Docker and Kubernetes build higher-level networks from those parts. In this lesson, you will inspect a Docker bridge and see where CNI fits in Kubernetes.
Network namespaces
A network namespace is an isolated view of network interfaces, addresses, routes, firewall rules, and sockets.
It is not a separate kernel. A process in the namespace sees its own eth0 and route table, while the host has
another view of the connected virtual interface.
A veth pair is like a virtual cable with two ends. One end can live in the container namespace; the other can attach to a Linux bridge on the host. The bridge forwards frames between its ports. The host then routes traffic outside the bridge and may translate addresses. That last step depends on host configuration.
veth pairs and bridges
Docker's bridge driver creates an isolated bridge network. Containers on the same user-defined network can reach each other and use network aliases. Published ports create a path from a host port to a container port; peer containers do not need published ports just to communicate on that network.
$ getent hosts api 172.19.0.2 api api $ ip -br addr lo UNKNOWN 127.0.0.1/8 ::1/128 eth0@if109 UP 172.19.0.3/16 $ ip route default via 172.19.0.1 dev eth0 172.19.0.0/16 dev eth0 proto kernel scope link src 172.19.0.3 api service
These lines came from the temporary lab. The last request used the address returned by getent; the numeric
address changes between networks. A frequent mistake is publishing a database port to the host even though only
peer containers need it. Keep the service on a private bridge instead.
Docker networks
Use a named network to make the boundary visible and repeatable:
docker network create app-net docker run -d --name api --network app-net --network-alias api nginx:alpine docker network inspect app-net
This example starts a short-lived Nginx service. Remove it and the network after testing. Do not use the legacy default bridge as a substitute for explicit service discovery; behavior differs from a user-defined network.
Pod networking and CNI
Kubernetes groups containers into a Pod. Containers in one Pod share a network namespace and one Pod IP, so
they can reach each other over localhost using different ports. A Container Network Interface (CNI) plugin
sets up the Pod's interface and the cluster network. The implementation varies by plugin and environment.
A Service is a separate abstraction. It gives clients a stable virtual address and selects ready Pods behind it. CNI provides connectivity; it does not, by itself, define the Service API. This lesson uses diagrams instead of a cluster lab so the exercise stays local and lightweight.
Inspect a Docker bridge
Create one user-defined network, resolve an alias, inspect the client's route, and request the service by its resolved address. Remove the container and network when done.
The resolved alias, the client route, and a response from the serviceReveal one safe solution
bash devops-academy-tools/networking-lab/11-capture.sh
The script records the client and service output in devops-academy-tools/networking-lab/outputs/11-container-networking.txt.
Its exit trap removes the service container and network.
Try it yourself
0 of 4 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
What does a Linux network namespace give a container?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.