Sub topic 13
Overlay Networks Across Hosts
This chapter assumes you can run containers with docker run, know what a user-defined bridge network is, and understand IP addresses, subnets, and ports. It covers how Docker connects containers that run on different hosts.
The problem a bridge cannot solve
A bridge network exists on one host. Containers on host-a and host-b cannot share a bridge, so a container on one cannot reach a container on the other by name or by private IP. Publishing ports works, but then you must track host addresses and ports yourself.
An overlay network is a virtual network that spans several Docker hosts. Containers attached to it get addresses on one shared subnet and can talk to each other as if they were on the same LAN. Docker also provides the same DNS-based name resolution you saw on user-defined bridges.
How it works
Overlay networks run on top of the existing host network. Docker wraps each container packet in a VXLAN packet (a UDP encapsulation of Layer 2 frames). The wrapped packet travels between hosts like any other UDP traffic, and the receiving host unwraps it and delivers the original packet to the target container.
Overlay networks depend on swarm mode, which supplies the key-value store that records which container lives on which host. Even if you never deploy swarm services, each host must be part of the same swarm.
The hosts must be able to reach each other on these ports:
| Port | Protocol | Purpose |
|---|---|---|
| 2377 | TCP | Swarm cluster management (manager nodes) |
| 7946 | TCP and UDP | Node-to-node discovery |
| 4789 | UDP | VXLAN data traffic |
Creating an overlay network
Initialize swarm mode on the first host, then create the network. The --attachable flag lets standalone containers (started with docker run) join, not only swarm services.
docker swarm init
docker network create --driver overlay --attachable my-overlaydocker swarm init prints a docker swarm join command with a token. Run that command on the second host so it joins the swarm. Then start a container on each host:
# On host A
docker run -d --name web --network my-overlay nginx:alpine
# On host B
docker run --rm --network my-overlay curlimages/curl http://webThe second command resolves web through Docker's embedded DNS and reaches the container on host A.
Encryption and common errors
Overlay data traffic is not encrypted by default. To encrypt it with IPsec, add -o encrypted when you create the network. This costs CPU and throughput, and the host firewall must also allow IP protocol 50 (ESP).
docker network create --driver overlay --attachable -o encrypted secure-overlayCommon problems:
- Name resolution or connections fail across hosts. A firewall is usually blocking 7946 or 4789. Check these ports between hosts first.
- "This node is not a swarm manager" when creating a network. Overlay networks must be created on a manager node.
- Standalone
docker runfails with "not manually attachable." The network was created without--attachable. Recreate it with the flag. - Subnet overlaps. If the overlay subnet collides with a host or VPN subnet, routing breaks. Set one explicitly with
--subnet.
When to use it
Use an overlay network when containers on separate hosts need private, name-based communication. For containers on a single host, a user-defined bridge is simpler and faster. For larger fleets, orchestrators such as Kubernetes use their own network plugins, so this approach mainly applies to Docker Swarm and small multi-host setups.
Exercise
Containers on two swarm hosts cannot reach each other over an overlay network, although docker network ls shows the network on both. Which ports and protocols would you verify between the hosts?
Show answer
Verify TCP 7946 and UDP 7946 for node discovery, UDP 4789 for VXLAN data traffic, and TCP 2377 for management traffic to the manager nodes. A firewall blocking any of these is the most common cause.
Exercise
You run docker run --network my-overlay nginx:alpine and Docker reports the network is not manually attachable. What is the fix?
Show answer
Recreate the network with docker network create --driver overlay --attachable my-overlay. Without --attachable, only swarm services can join it.