Skip to main content

Swarm Networking

Imagine a city with multiple districts (nodes). To keep traffic flowing, you build highways (overlay networks) that connect all districts. No matter where people live, they can reach each other smoothly. In Docker Swarm, Swarm networking works the same way — it connects containers across nodes, balances traffic, and ensures services can find each other by name.


Networking Foundations

1. Networking in Swarm

  • Swarm uses overlay networks to connect containers across nodes.
  • Each service gets a virtual IP (VIP) for load balancing.
  • Built‑in DNS allows services to resolve each other by name.

2. Key Networking Features

  • Overlay Networks: Enable cross‑node communication.
  • Ingress Network: Default network for exposing services externally.
  • Service Discovery: Containers resolve services by name (e.g., db).
  • Load Balancing: Requests are distributed across replicas automatically.

3. Ingress vs Custom Overlay Networks

  • Ingress Network:
    • Automatically created by Swarm.
    • Handles external traffic routing to services.
  • Custom Overlay Networks:
    • User‑defined for internal service communication.
    • Provide isolation between applications.

4. Swarm Networking Workflow

  1. Initialize Swarm cluster.
  2. Create an overlay network.
  3. Deploy services attached to the network.
  4. Swarm handles DNS resolution and load balancing.

Things to Remember

  • Swarm networking enables multi‑host communication with built‑in DNS and load balancing.
  • The ingress network handles external traffic, while custom overlay networks handle internal communication.
  • Service names act as hostnames, eliminating the need for IP addresses.

Hands‑On Lab

Step 1: Create an Overlay Network

docker network create -d overlay appnet

Step 2: Deploy Services in the Overlay Network

docker service create --name web --network appnet -p 8080:80 nginx
docker service create --name db --network appnet redis

Step 3: Verify Networking

docker network ls
docker network inspect appnet

Step 4: Test Service Discovery
Inside the web container:

ping db
  • Resolves db by name.

Step 5: Observe Load Balancing
Access http://<node_ip>:8080 multiple times.

  • Requests are distributed across replicas.

Practice Exercise

  1. Create a custom overlay network swarmnet.
  2. Deploy two services (frontend and backend) in swarmnet.
  3. Expose frontend on port 8081.
  4. Verify that frontend can resolve backend by name.
  5. Scale frontend to 3 replicas and observe load balancing.

Visual Learning Model

Swarm Cluster
   ├── Ingress Network (external traffic)
   └── Overlay Network: appnet
         ├── Service: web (nginx)
         └── Service: db (redis)
DNS + Load Balancing ensure seamless communication

The Hackers Notebook

Swarm networking connects containers across nodes using overlay networks, built‑in DNS, and load balancing. The ingress network handles external traffic, while custom overlay networks provide internal communication. Service names act as hostnames, making networking simple and scalable in distributed environments.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 26, 2025