Swarm services, replicas & load balancing

Docker Swarm - Overview and how to use it

3 min read

Published Jul 13 2025, updated Jul 14 2025


7
0
0
0

CLIDockerMulti-NodeOrchestrationReplicasServicesStacks

In Docker Swarm, a service is the primary unit of deployment and scaling. While a regular docker run command starts a single container, a Swarm service lets you define and manage a group of containers that should be running across your Swarm cluster.


A service is a higher-level abstraction, and it comes in two main modes:

  • Replicated services – Define how many copies (replicas) of a container you want.
  • Global services – Run exactly one task per node (useful for logging or monitoring agents).

You can think of a Swarm service as a self-healing, load-balanced, declarative blueprint for how your containers should behave in a cluster environment.





Replicated Services

A replicated service in Swarm maintains a desired number of identical containers (tasks), and Swarm ensures that the correct number is always running. If a container crashes or a node goes down, the Swarm manager reschedules it on a healthy node.

Example:

docker service create \
  --name webapp \
  --replicas 3 \
  -p 80:80 \
  nginx

This command tells Swarm:

  • Run 3 replicas of the nginx container
  • Distribute them across the available nodes
  • Map port 80 on the host to port 80 on each container

Swarm will orchestrate container placement, ensure even distribution, and monitor container health. If any replica dies, Swarm will restart it automatically.


You can change the number of replicas any time:

docker service scale webapp=5

This scales the webapp service to 5 containers. Swarm handles this gracefully — adding or removing containers without downtime.





Load Balancing in Swarm

Swarm has built-in internal load balancing that makes sure traffic is routed evenly to all service replicas.

There are two types of load balancing:


Ingress Load Balancing

  • Works automatically when you publish a port with -p or --publish.
  • Docker sets up a routing mesh: any node in the cluster can receive traffic on the published port and route it to the correct service replica.
  • Even if a service is not running on that specific node, the node will forward traffic over the overlay network.

Example:

docker service create \
  --name myapp \
  --replicas 4 \
  --publish published=8080,target=80 \
  myapp-image

Even if a replica isn't running on node-1, node-1 will still accept traffic on port 8080 and forward it to a running replica via the routing mesh.



DNS-Based Load Balancing

Every Swarm service also gets its own internal DNS entry. If containers resolve the service name, Docker returns a round-robin list of IP addresses for each task (replica). This is used for inter-service communication within the Swarm.


Example:

A container querying http://webapp inside the Swarm will resolve the name to one of the running replicas of the webapp service.





Service Management

You can inspect services and their state with:

docker service ls

View detailed service info:

docker service inspect webapp --pretty

Check which nodes the tasks are running on:

docker service ps webapp

This shows each replica (task), its state, and on which node it is running. If a node goes down, you’ll see Swarm rescheduling the task to another node.





Behind the Scenes: How Swarm Maintains State

Each Swarm manager stores the desired state of the cluster in a Raft-based distributed store. When you define a service with 5 replicas, the Raft consensus ensures that the state is recorded as "5 replicas." If something deviates (e.g., a node dies), the managers detect the inconsistency and bring the cluster back to the desired state.


This self-healing behaviour is one of Swarm’s biggest strengths.





Global Services

As mentioned earlier, global services run exactly one container per node, which is great for monitoring/logging tools like:

docker service create \
  --mode global \
  --name node-exporter \
  prom/node-exporter

Each node runs one replica, and when a new node joins the Swarm, Swarm automatically runs a new instance of the global service on it.


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact