The network model: CNM, libnetwork and drivers
You have now seen namespaces, a bridge, veth pairs and NAT. Docker has one vocabulary for all of them (sandbox, endpoint, network), one engine that builds them (libnetwork), and plug-in drivers that decide how packets move.
? What do the diagram colours mean
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
Three nouns
Map them onto what you already know: the sandbox is the namespace from chapter 12, an endpoint is one end of a veth pair, and the network is the bridge from chapter 13. The next two chapters (host/none/macvlan and overlay) are variations on the same three nouns.
| Object | What it is | Real-world analogy | You see it as |
|---|---|---|---|
| Sandbox | the container network namespace | a computer | docker inspect → NetworkSettings |
| Endpoint | a virtual interface with IP and MAC | a network card | an entry under Networks |
| Network | a group of endpoints that can talk | a switch | docker network ls |
A container on two networks
Building a connection
From the CLI to the kernel
Choosing a driver
There are only a handful, and each answers one question.
| Driver | Scope | Container gets | Use it when |
|---|---|---|---|
bridge | local | private IP behind NAT | the default for one machine |
host | local | the host's own stack | you need raw speed and accept no isolation |
overlay | swarm (multi-host) | IP on a virtual multi-host network | containers on different hosts must talk |
macvlan | local | its own MAC and LAN IP | it must look like a real machine on the LAN |
ipvlan | local | a LAN IP, sharing the parent MAC | macvlan, on a network that limits MACs |
none | local | only loopback | full network isolation |
Pluggable
Try it yourself
-
Step 1 · List networks and their drivers
$ docker network ls NETWORK ID NAME DRIVER SCOPE a1b2c3d4e5f6 bridge bridge local b2c3d4e5f6a1 host host local c3d4e5f6a1b2 none null local -
Step 2 · Create the network, then one container
The sandbox is the container. This command gives it one endpoint, on the network named
payments.$ docker network create --driver bridge payments $ docker run -d --name api --rm --network payments alpine:3.20 sleep 300 -
Step 3 · Read that endpoint
$ docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{println}}{{end}}' api payments 172.18.0.2 -
Step 4 · Add a second endpoint
network connectdoes not replace the first address. The sandbox now has two.$ docker network connect bridge api $ docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{println}}{{end}}' api payments 172.18.0.2 bridge 172.17.0.2 $ docker stop api $ docker network rm payments
Recap
- A sandbox is the container network namespace; an endpoint is one interface; a network joins endpoints.
- An endpoint belongs to exactly one sandbox and one network.
- A container can have many endpoints, so many IPs.
- libnetwork implements CNM inside the daemon; a driver (bridge, host, overlay, macvlan, ipvlan, none) decides how packets move.
localnetworks live on one host;swarm-scoped networks span hosts.
Quick check
You run docker network connect bridge api on a container already on payments. How many IPs does it have?
Show answer
Answer: Two: one endpoint per network.
Connecting adds a second endpoint on the bridge network; the first endpoint stays.
In an interview: “How does Docker networking work?” Which answer shows real understanding?
Show answer
Answer: The CNM model, implemented by libnetwork, which calls a driver to build veth pairs, bridges and iptables rules.
Describe the layers: model (sandbox, endpoint, network), engine (libnetwork), driver (bridge, overlay…), kernel objects.