Networking: CNM (sandbox, endpoint, network)
CNM is Docker's mental model for networking: three nouns that every driver implements.
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
Three nouns
Learn these once and every later chapter (bridge, overlay, macvlan) is a variation.
| 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
Try it yourself
-
Step 1 · 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 payments $ docker run -d --name api --rm --network payments alpine:3.20 sleep 300 -
Step 2 · Read that endpoint
$ docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{println}}{{end}}' api payments 172.18.0.2 -
Step 3 · 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.
- CNM is the model. Drivers implement it.
Quick check
You run
docker network connect bridge api on a container already on payments. How many IPs does it have?Show answer
Two. Connecting adds a second endpoint on the bridge network; the first endpoint stays.