Stage 3 · Network internals · Chapter 15 of 20 · 6 min read

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.

The Container Network ModelA sandbox contains endpoints. Each endpoint joins exactly one network. Two sandboxes on the same network can talk.Sandbox A (container)Endpoint172.18.0.2Sandbox B (container)Endpoint172.18.0.3NetworkpaymentsSandboxthe computerEndpointthe network cardNetworkthe switch
Sandbox = the computer, endpoint = its network card, network = the switch.
ObjectWhat it isReal-world analogyYou see it as
Sandboxthe container network namespacea computerdocker inspect → NetworkSettings
Endpointa virtual interface with IP and MACa network cardan entry under Networks
Networka group of endpoints that can talka switchdocker network ls

A container on two networks

One sandbox, two endpointsThe api container has two endpoints. One is on the payments network, one on the default bridge, so it has two IP addresses.Sandbox: container apiEndpoint 1172.18.0.2Endpoint 2172.17.0.2Network: paymentsuser-defined bridgeNetwork: bridgedefault docker0
docker network connect adds an endpoint. It does not replace the first one.

Building a connection

How it is built, step by stepnetwork createpaymentsrun --networkpaymentsDocker makesan endpointPlugs sandboxinto network
Docker creates the endpoint and plugs the sandbox in; you only name the network.

From the CLI to the kernel

From docker CLI to the kernelThe CLI calls the daemon. The daemon uses libnetwork, which implements CNM and calls a driver. The driver configures the Linux kernel.docker CLIdocker run --network appDocker daemon (dockerd)libnetworkimplements the CNM: sandbox, endpoint, networkDrivers (plugins)bridgesingle hosthostno isolationoverlaymany hostsmacvlanown MACnullno networkLinux kernelnamespaces · veth · bridges · iptables · VXLAN
libnetwork is the layer that turns the CNM model into real interfaces and rules.

Choosing a driver

There are only a handful, and each answers one question.

Which driver do I need?Where do the containers run?bridgedefault, localhostno isolationmacvlanown LAN IPoverlaymany hostsnoneloopback only
Start at bridge. Move right only when you have a reason.
DriverScopeContainer getsUse it when
bridgelocalprivate IP behind NATthe default for one machine
hostlocalthe host's own stackyou need raw speed and accept no isolation
overlayswarm (multi-host)IP on a virtual multi-host networkcontainers on different hosts must talk
macvlanlocalits own MAC and LAN IPit must look like a real machine on the LAN
ipvlanlocala LAN IP, sharing the parent MACmacvlan, on a network that limits MACs
nonelocalonly loopbackfull network isolation

Pluggable

Pluggable by designlibnetwork talks to every driver through the same interface. Built-in drivers and third-party plugins plug into it the same way.libnetworkone driver interfacedriver APIBuilt-in driversbridge · host · overlay · macvlan · nullThird-party network pluginsinstalled separately, same interface

Try it yourself

  1. 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
  2. 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
  3. Step 3 · Read that endpoint
    $ docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{println}}{{end}}' api
    payments 172.18.0.2
  4. Step 4 · Add a second endpoint

    network connect does 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
Common mistakeReaching for macvlan or host networking to “fix” something a user-defined bridge already solves. Start at bridge, and move on only for a concrete reason.

Recap

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.