Skip to main content

Overlay Network

A Docker overlay network is a multi-host network driver that lets containers on different Docker hosts communicate as if on one network, using VXLAN tunnels. It is used with Docker Swarm.

How overlay networks work

A bridge network connects containers on one host. An overlay network spans hosts: container traffic is wrapped in VXLAN packets (UDP) and carried across the real network between nodes, then unwrapped and delivered. Containers see a flat network and can resolve each other by service name.

flowchart LR subgraph h1 [Host 1] A[api 10.0.0.2] end subgraph h2 [Host 2] W[worker 10.0.0.3] end A <-->|VXLAN tunnel| W classDef c fill:#DBEAFE,color:#1E293B class A,W c

Create and use

Bash
docker swarm init
docker network create --driver overlay --attachable payment-overlay
docker service create --name payment-api --network payment-overlay \
registry.example.com/payment-api:v3.1.0

--attachable lets standalone containers join the network, not only Swarm services.

Encrypted overlay

Bash
docker network create --driver overlay --opt encrypted --attachable secure-overlay

Traffic between hosts is encrypted with IPsec, which adds some CPU overhead.

Firewall requirements

Between Swarm nodes, allow TCP 2377 (cluster management), TCP and UDP 7946 (node communication), and UDP 4789 (VXLAN data). Mismatched MTU on the underlying network is a common cause of strange packet loss.

Connection to Kubernetes

Kubernetes CNI plugins such as Flannel (VXLAN mode), Calico, and Cilium solve the same cross-host networking problem. Understanding overlay encapsulation helps when debugging them.

Remember

Overlay networks need Swarm mode. For a single host, use a bridge network.

Frequently Asked Questions

How do overlay networks connect containers on different hosts?

Traffic is encapsulated in VXLAN packets and tunneled between hosts in the Swarm. Each container gets an address on the overlay subnet and Docker DNS resolves service names, so containers talk without per-host routes.

What ports must be open for an overlay network?

TCP 2377 for cluster management, TCP and UDP 7946 for node communication, and UDP 4789 for VXLAN data traffic between nodes.

Why learn overlay networks if I use Kubernetes?

CNI plugins implement the same idea of encapsulated cross-node networking, so concepts like VXLAN overhead, MTU, and required UDP ports transfer directly to Kubernetes debugging.

Found a mistake, an outdated step or a broken link?