In this lesson, you will learn how to enable seamless communication between containers running on different physical machines within your Swarm cluster using overlay networks.
The Problem of Physical Boundaries
When you run containers on a single host, they can easily talk to each other using local networking. However, in a multi-node Swarm cluster, those containers are physically isolated on different servers. Without a shared networking layer, a container on Node A would have no way of reaching a container on Node B using standard private IP addresses.
The Overlay Network Solution
An overlay network acts like a virtual "tunnel" stretched across your physical nodes. It creates a distributed, private network that spans every node in your swarm. When you attach services to this network, Docker handles the underlying complexity of routing traffic through the cluster. To the containers, it feels as if they are all plugged into the same local switch, even if one is in a datacenter in New York and another is in London.
Implementation
To use this, you must first create an overlay-type network. Once created, any service you deploy can join this network, allowing them to resolve each other by name (using Docker’s internal DNS) regardless of where they are placed by the orchestrator.
# Create the network with the overlay driver
docker network create --driver overlay my-distributed-app
# Deploy a service attached to that specific network
docker service create \
--name web-app \
--network my-distributed-app \
nginx:latest
When you attach your database service and your web service to the same my-distributed-app network, the web service can simply reach the database using its service name (e.g., http://database:5432). Docker’s overlay driver automatically encapsulates the traffic, sends it across the physical network to the correct node, and delivers it to the destination container. This provides a secure, private communication channel that is isolated from the rest of your infrastructure.
Key Takeaway: Overlay networks remove physical limitations, allowing containers to communicate as if they share the same host by creating a virtual, cluster-wide network layer.