7.2.4.2 Overlay Swarm Services
A focused guide to Overlay Swarm Services, connecting core concepts with practical Docker and container operations.
Overlay Swarm services are Docker Swarm-managed services that use overlay networks as their primary networking mechanism, allowing multiple replicas of a service, potentially spread across many hosts in the cluster, to communicate with each other and with other services seamlessly.
Creating a Service on an Overlay Network
A Swarm service is created with an associated overlay network, with each of its replicas automatically joining that network regardless of which specific host they end up scheduled on.
docker network create --driver overlay app-overlay
docker service create --name api --network app-overlay --replicas 3 myapi:1.0
Each of the three api replicas, wherever they happen to be scheduled within the cluster, joins app-overlay and can communicate with other services on it.
How Service Discovery Works Across Replicas
A service name resolves to a virtual IP that load balances across all of that service's current replicas, providing built-in service discovery and basic load balancing without additional configuration.
docker service create --name db --network app-overlay postgres:16
docker exec $(docker ps -q --filter name=api) ping db
This resolves to a virtual IP representing the db service as a whole, rather than any one specific replica's individual address.
Scaling a Service Without Changing Its Networking
Because service discovery is handled at the service level rather than per-replica, scaling a service up or down doesn't require any change to how other services reference it.
docker service scale api=10
Other services continue reaching api by its service name exactly as before, regardless of how many replicas are currently running.
Why Overlay Swarm Services Matter
The combination of overlay networking and Swarm's service abstraction provides resilient, scalable, multi-host service communication with built-in discovery and load balancing, making it practical to design and run genuinely distributed applications without manually managing the underlying multi-host networking complexity.