Skip to content

Docker overlay networks and Swarm — multi-host container networking

How Docker overlay networks span hosts via VXLAN, the encrypted-data-path option, and the Swarm bits you need (init, join, services) to make them work.

When two containers need to talk and they’re on different hosts, the single-host bridge driver doesn’t help. Docker’s overlay driver creates a virtual L2 network that spans hosts using VXLAN encapsulation. It’s the foundation under Swarm services, and even without Swarm, it’s the answer when you want container-to-container DNS across machines. This article covers when overlay is the right tool, the Swarm setup it requires, and the operational notes that come up in real deployments.

How to verify

Inspect Swarm state and overlay networks on a node:

docker info | grep -E "Swarm|Is Manager|Nodes"
docker node ls
docker network ls --filter driver=overlay
docker network inspect <overlay-net> | jq '.[].Containers'

# VXLAN interface on the host
ip -d link show | grep -A2 vxlan
sudo nft list table inet swarm-ingress 2>/dev/null

If docker info reports Swarm: inactive, overlay networks won’t work — they require Swarm’s KV store to coordinate state. docker network ls should show ingress (Swarm’s built-in overlay) plus any user-defined overlay networks you’ve created.

What’s happening

An overlay network is implemented as a VXLAN tunnel between each pair of hosts that has a container on the network. Each container gets a virtual interface in the overlay’s subnet (e.g. 10.0.1.0/24); when the container sends a packet to a peer on another host, the packet is wrapped in a VXLAN header (UDP port 4789), sent to the peer host’s IP, unwrapped, and delivered to the destination container.

Swarm provides the control plane: a Raft-replicated KV store that tells every node which containers are on which host and what their overlay IPs are. Without Swarm, Docker has no way to coordinate this — so even though --driver overlay works without Swarm, you’d need to bring your own KV store (Consul, etcd), which nobody does in 2026.

Encryption is optional: --opt encrypted on the overlay enables IPSec on the VXLAN traffic. Slower (5-15% throughput hit, more CPU), but the data path between hosts is encrypted in transit. Useful when overlay traffic crosses an untrusted network (e.g. between cloud regions, between on-prem and cloud).

The procedure

  1. Initialize Swarm on the first node. This becomes the initial manager:
# On node-1
docker swarm init --advertise-addr <node-1-IP>
# Output includes a join token; save it

--advertise-addr is the IP other nodes will use to reach this one. Use the private/internal IP, not the public one, when nodes are in the same network.

  1. Join other nodes to the swarm:
# On node-2, node-3 — worker join token from step 1
docker swarm join --token SWMTKN-1-... <node-1-IP>:2377

# To add another manager (always run 3 or 5 managers for quorum)
docker swarm join-token manager   # on existing manager
# Then run the printed command on the new manager candidate

Ports that must be open between Swarm nodes:

  • 2377/tcp — cluster management API
  • 7946/tcp and 7946/udp — node discovery (gossip)
  • 4789/udp — VXLAN data path
  • 50/ip — IPSec ESP (only if --opt encrypted)
  1. Create an encrypted overlay network for application traffic:
docker network create \
  --driver overlay \
  --opt encrypted \
  --subnet 10.20.0.0/24 \
  --attachable \
  app-net

--attachable lets you run docker run --network app-net on any node (not just Swarm services). Without it, only docker service workloads can attach.

  1. Deploy a Swarm service that spans nodes:
docker service create \
  --name web \
  --replicas 3 \
  --network app-net \
  --publish published=8080,target=8080 \
  --update-parallelism 1 \
  --update-delay 30s \
  --update-failure-action rollback \
  --restart-condition any \
  ghcr.io/example/web:1.42.0

Swarm distributes the 3 replicas across nodes. The publish flag uses Swarm’s ingress network — any node receiving traffic on port 8080 routes it to whichever replica is healthy (round-robin via the ingress routing mesh).

  1. Inspect what Swarm did:
docker service ls
docker service ps web
docker service inspect web --pretty
docker service logs web --tail 50

# Per-node view
docker ps                          # only containers on the current node
docker node ps <node-name>         # containers Swarm placed on a specific node
  1. Update a service. Swarm orchestrates a rolling update based on the update policy you set at create time:
docker service update --image ghcr.io/example/web:1.43.0 web

# Watch the rollout
docker service ps web --filter desired-state=running

--update-parallelism 1 --update-delay 30s means one task at a time, 30 seconds between batches. If a batch fails, --update-failure-action rollback reverts to the previous image.

  1. Use Docker Stack to deploy from a Compose file:
# Same compose.yaml works, with a few Swarm-only keys
docker stack deploy -c compose.yaml myapp
docker stack services myapp
docker stack ps myapp
docker stack rm myapp

The deploy: block in Compose is consumed by Swarm:

services:
  web:
    image: ghcr.io/example/web:1.42.0
    deploy:
      replicas: 3
      restart_policy:
        condition: any
        delay: 5s
      update_config:
        parallelism: 1
        delay: 30s
        failure_action: rollback
      resources:
        limits:
          memory: 512m
          cpus: '1.0'
      placement:
        constraints:
          - node.role == worker
    networks:
      - app-net

networks:
  app-net:
    driver: overlay
    attachable: true
    encrypted: true
  1. Lock the Swarm. Without autolock, the Raft state is encrypted with a key stored on disk in plaintext — anyone with root on a manager has full cluster access. With autolock, the key is removed and must be supplied at daemon start:
docker swarm update --autolock=true
# Save the unlock key it prints
# After daemon restart:
docker swarm unlock
# Paste the unlock key

Common pitfalls

  • Forgetting to open the Swarm ports between nodes — services start but nothing can talk to anything. Verify with sudo nft list ruleset (or iptables-save) and a netcat probe between hosts.
  • Running overlay without --opt encrypted on networks that cross the public internet. The default is unencrypted VXLAN; anyone with packet capture between the hosts sees the container traffic.
  • Skipping autolock and losing the cluster the first time a manager is compromised — the Raft KV store contains every secret, every service config.
  • Running 2 managers (no quorum) or 4 (even number). Always odd — 3 or 5.
  • Mixing docker service and docker run on the same overlay without --attachable. Plain docker run will refuse to connect, and you’ll have a confusing dual-deploy mess.
  • Letting the ingress network become the bottleneck. The routing mesh is convenient but routes every request through whichever node received the packet; for high-throughput services, run a real load balancer in front of Swarm and use the long-form publish mode=host to bypass the mesh.

For most clients, we’ve moved past Swarm — Kubernetes (or a managed PaaS) is where multi-host orchestration lives in 2026. Swarm remains a fit for small fleets (3-10 nodes) where the operational simplicity outweighs the feature gap. When we run Swarm clusters, autolock, encrypted overlays, and the Compose-stack workflow are all part of the managed operations baseline.