Overlay Networks
On a single Docker host, containers talk over a bridge network: a virtual switch inside one Linux kernel (see Docker networks). In a swarm the replicas of a service are spread over many machines, yet api on node 1 still needs to reach db on node 3 as if they sat on the same switch. Swarm solves this with overlay networks: virtual layer-2 networks stretched across every node that runs a task attached to them.
What an overlay network is
An overlay is a private network that exists "on top of" the real network between the nodes (the underlay). Each container gets an address in the overlay subnet (say 10.0.1.0/24). When it sends a packet to another container on a different node, the local Docker engine wraps the whole Ethernet frame in a UDP packet and sends it to the other node's real IP. The receiving engine unwraps it and delivers the original frame. The wrapping format is VXLAN (Virtual eXtensible LAN), a standard also used by data-centre switches.
node-1 (192.168.10.11) node-3 (192.168.10.13)
┌───────────────────────┐ ┌───────────────────────┐
│ api.1 10.0.1.5 │ │ db.1 10.0.1.9 │
│ │ │ │ ▲ │
│ ▼ overlay "back" │ UDP 4789 (VXLAN) │ │ overlay "back" │
│ vxlan0 ── eth0 ───────┼──────────────────────┼─► eth0 ── vxlan0 │
└───────────────────────┘ outer: .11 → .13 └───────────────────────┘
inner: 10.0.1.5 → 10.0.1.9Each overlay network gets its own VXLAN Network Identifier (VNI), a 24-bit number in the VXLAN header. Two overlays can share the same wire without ever seeing each other's frames, because the receiving engine only delivers a frame to the network whose VNI it carries.
Let's model the encapsulation and count the bytes it costs:
OUTER = {"ethernet": 14, "ipv4": 20, "udp": 8, "vxlan": 8}
overhead = sum(OUTER.values())
def encapsulate(frame, vni, src_node, dst_node):
return {"outer_src": src_node, "outer_dst": dst_node, "udp_dport": 4789,
"vni": vni, "inner": frame}
def decapsulate(packet, local_networks):
net = local_networks.get(packet["vni"])
if net is None:
return "dropped: no local network with VNI %d" % packet["vni"]
return "delivered on %s: %s -> %s" % (net, packet["inner"]["src"], packet["inner"]["dst"])
frame = {"src": "10.0.1.5", "dst": "10.0.1.9", "payload": b"GET /orders"}
pkt = encapsulate(frame, vni=4097, src_node="192.168.10.11", dst_node="192.168.10.13")
print("outer:", pkt["outer_src"], "->", pkt["outer_dst"], "udp/%d" % pkt["udp_dport"], "vni", pkt["vni"])
node3 = {4097: "shop_back", 4098: "shop_front"}
print(decapsulate(pkt, node3))
print(decapsulate(encapsulate(frame, 4200, "a", "b"), node3))
print("\nVXLAN overhead per packet:", overhead, "bytes", OUTER)
for underlay_mtu in (1500, 9000):
print(f"underlay MTU {underlay_mtu} -> overlay MTU {underlay_mtu - overhead}")outer: 192.168.10.11 -> 192.168.10.13 udp/4789 vni 4097
delivered on shop_back: 10.0.1.5 -> 10.0.1.9
dropped: no local network with VNI 4200
VXLAN overhead per packet: 50 bytes {'ethernet': 14, 'ipv4': 20, 'udp': 8, 'vxlan': 8}
underlay MTU 1500 -> overlay MTU 1450
underlay MTU 9000 -> overlay MTU 8950
That 50-byte overhead is why overlay interfaces use an MTU of 1450 on a normal 1500-byte network. If your underlay itself is smaller (some VPNs and cloud tunnels use 1400 or less), set the overlay MTU explicitly with the driver option com.docker.network.driver.mtu, or large packets will vanish while small ones (ping, DNS) still work: a classic "it half works" symptom.
The networks a swarm creates for you
| Network | Driver | Purpose |
|---|---|---|
ingress |
overlay | carries traffic for published ports: the routing mesh |
docker_gwbridge |
bridge (per node) | connects overlay containers to the node's own network, so they can reach the internet |
<stack>_default |
overlay | created by docker stack deploy when services don't name a network |
| your own | overlay | docker network create -d overlay … or a networks: entry in a stack file |
Overlay subnets come from the swarm's default address pool, 10.0.0.0/8 carved into /24 networks unless you chose otherwise at docker swarm init --default-addr-pool 10.20.0.0/16 --default-addr-pool-mask-length 26. Pick a pool that doesn't collide with your office or cloud VPC ranges: a container can't reach a real host whose address is inside one of its overlays.
Ports to open between nodes
| Port | Protocol | Used for |
|---|---|---|
| 2377 | TCP | cluster management (to managers only) |
| 7946 | TCP + UDP | node gossip: which tasks and IPs live where |
| 4789 | UDP | VXLAN data traffic |
| IP protocol 50 (ESP) | encrypted overlays (IPsec) |
A firewall that blocks 4789/udp produces a swarm where everything looks healthy (docker node ls is green) but containers on different nodes can't talk.
Overlays in a stack file
In a stack file, networks default to the overlay driver, and each service lists the networks it joins. A service on two networks gets an interface in both, which is how you build tiers: the proxy sees the API, the API sees the database, and the proxy never sees the database.
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
proxy:
image: nginx:1.27-alpine
networks: [front]
api:
image: ghcr.io/acme/shop-api:2.4.1
networks: [front, back]
db:
image: postgres:17
networks: [back]
networks:
front:
driver: overlay
back:
driver: overlay
internal: true
driver_opts:
encrypted: "true"
com.docker.network.driver.mtu: "1400"version: "3.13"
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
networks:
back: null
front: null
db:
image: postgres:17
networks:
back: null
proxy:
image: nginx:1.27-alpine
networks:
front: null
networks:
back:
driver: overlay
driver_opts:
com.docker.network.driver.mtu: "1400"
encrypted: "true"
internal: true
front:
driver: overlay
internal: true gives the network no route to the outside world (no docker_gwbridge interface), a useful default for a database tier. encrypted turns on IPsec (AES-GCM) between nodes for this network's data traffic; the keys are generated by the managers and rotated automatically every 12 hours. Encryption costs CPU and is not supported on Windows nodes, so enable it per network where the data deserves it rather than everywhere.
Attachable overlays
By default only swarm services can join an overlay. Mark it attachable: true and plain docker run --network containers can join too, which is handy for one-off debugging or migrations:
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
networks:
tools:
aliases: [orders-api]
networks:
tools:
driver: overlay
attachable: true
edge:
external: true
name: traefik-publicversion: "3.13"
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
networks:
tools:
aliases:
- orders-api
networks:
edge:
name: traefik-public
external: true
tools:
driver: overlay
attachable: true
$ docker run --rm -it --network shop_tools nicolaka/netshoot dig +short orders-api
10.0.4.2The edge network is external: the stack doesn't create it, it expects a network called traefik-public to exist already (typically created once by the cluster admin and shared by several stacks). The aliases entry adds extra DNS names for the service on that network only.
Inspecting overlays
$ docker network ls --filter driver=overlay
NETWORK ID NAME DRIVER SCOPE
q3p0r0oz1ua2 ingress overlay swarm
w7m9k2c4x1d8 shop_back overlay swarm
f2t6n8b1e5j0 shop_front overlay swarm
$ docker network inspect shop_back --format '{{json .IPAM.Config}} {{index .Options "com.docker.network.driver.overlay.vxlanid_list"}}'
[{"Subnet":"10.0.2.0/24","Gateway":"10.0.2.1"}] 4098(sample output; IDs, subnets and VNIs differ on your cluster.) An overlay is created lazily on worker nodes: a worker only sees shop_back in docker network ls while it runs a task attached to it.
Tip: Compose (single host) uses bridge networks for the same
networks:keys; Compose networks explains the shared syntax. Kubernetes takes a different approach: one flat Pod network provided by a CNI plugin, segmented with NetworkPolicies.
Learn more: The Routing Mesh · Service Discovery & Load Balancing · Swarm Security
Test yourself
Which protocol and port carry the data traffic of a Swarm overlay network between nodes?
- TCP 2377
- UDP 4789 (VXLAN)
- TCP 7946
- TCP 443
Containers on different nodes can ping each other, but large HTTP responses hang. What is the most likely cause?
- The service has too few replicas
- An MTU mismatch: the underlay is smaller than the overlay MTU plus the 50-byte VXLAN overhead
- The network is not attachable
- The embedded DNS server is down
What does attachable: true on an overlay network allow?
- Encrypting the traffic
- Standalone
docker runcontainers to join the network - Publishing ports on every node
- Using the network from another swarm