4 Hours

13. Docker Swarm: Production Orchestration

Orchestration

Running Containers at Scale

Docker Compose is great for a single server. Docker Swarm turns a cluster of machines into a single virtual Docker host, handling service discovery, load balancing, rolling updates, and high availability.

Swarm Mode Concepts

A Docker Swarm consists of Manager nodes (control plane) and Worker nodes (run containers). Managers maintain the desired state of the cluster — how many replicas of each service should be running, what networks exist, what secrets are defined. If a worker node fails, the manager schedules its containers on healthy nodes. Swarm is built into Docker (no separate installation) and can be initialized with `docker swarm init`. For production, always run at least 3 manager nodes for high availability (etcd-style quorum). Swarm is simpler than Kubernetes but less feature-rich — ideal for smaller production deployments or teams that want built-in orchestration without managing complex control planes.

Bash
# Initialize swarm on first manager
docker swarm init --advertise-addr 192.168.1.10

# Get join token for workers
docker swarm join-token worker

# On worker node, join the swarm
docker swarm join --token SWMTKN-1-xxx 192.168.1.10:2377

# List nodes in swarm
docker node ls

# Promote a worker to manager
docker node promote worker-node-1

# Demote a manager to worker
docker node demote manager-node-3

Deploying Services in Swarm

Swarm services are the equivalent of `docker run` but distributed across the cluster. `docker service create` defines a service with desired replica count, network, ports, and update policy. Swarm ensures the desired number of replicas are always running. If a node goes down, Swarm reschedules its replicas on other nodes. Use `docker service update` to change configuration (image version, replica count, environment variables) without downtime — Swarm performs rolling updates.

Bash
# Create a service with 3 replicas
docker service create --name web --replicas 3 -p 80:80 nginx

# Scale to 5 replicas
docker service scale web=5

# Update image with rolling update
docker service update --image nginx:alpine web

# Configure rolling update parameters
docker service create \
  --name web \
  --replicas 10 \
  --update-parallelism 2 \
  --update-delay 10s \
  --update-failure-action rollback \
  nginx:latest

# View service details
docker service inspect --pretty web

# List services
docker service ls

# View service logs
docker service logs web -f

Routing Mesh and Ingress

Swarm's routing mesh allows you to publish a service's port on every node in the cluster. When a request arrives on any node at the published port, Swarm routes it to a healthy replica of the service, even if that replica is on a different node. This provides load balancing and high availability — you can put a simple load balancer (or even just DNS round-robin) in front of your swarm nodes. The routing mesh works for both `-p` published ports and for the internal overlay network.

Bash
# Publish port on all swarm nodes
docker service create --name web --publish 8080:80 --replicas 3 nginx

# Request to any node on port 8080 reaches a replica
docker service inspect web --format='{{json .Endpoint.Spec.Ports}}'

# Check which node is actually running the service
docker service ps web

Stacks: Deploying Compose Files to Swarm

Swarm supports a superset of Compose syntax called `docker stack`. A `docker-stack.yml` file looks almost identical to a `docker-compose.yml` file, but adds Swarm-specific options like `deploy` for replicas, update config, and placement constraints. Deploy your stack with `docker stack deploy`. Stacks are the Swarm equivalent of Compose projects — they group related services and can be updated atomically.

Text
# docker-stack.yml
version: '3.8'

services:
  web:
    image: myapp:latest
    deploy:
      replicas: 5
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
    ports:
      - "8080:8080"

  database:
    image: postgres:15-alpine
    deploy:
      placement:
        constraints:
          - node.role == manager
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:
Bash
# Deploy stack
docker stack deploy -c docker-stack.yml myapp

# List stacks
docker stack ls

# List services in stack
docker stack services myapp

# List tasks (containers) in stack
docker stack ps myapp

# Remove stack
docker stack rm myapp

Knowledge Check

Ready to test your understanding of 13. Docker Swarm: Production Orchestration?

Back