liveBook · Manning

Docker Swarm

As explained in Road to Kubernetes

Docker Swarm is Docker's native orchestration mode for managing services across a cluster of Docker engines.

Docker Compose can start several services on one machine. Docker Swarm takes a similar configuration and runs services across a cluster of machines. It is aimed at deployments rather than local development: you can check a configuration with Compose before deploying it with Swarm. section 9.1

A few terms make the cluster easier to follow:

  • A manager node decides what to run. By default, it can also run work itself. section 9.1
  • A worker node runs assigned tasks; a task is a running container. section 9.1
  • A service defines a task to run. A stack groups services for deployment to the cluster. section 9.1

From one machine to a cluster

Start with an NGINX service that sends / to a Python application and /api to a Node.js application. Check the services with docker compose up, then stop them. For Swarm, use a Compose file with a deploy.replicas count for each service and a bind-mount definition for nginx.conf. Initialize the manager with docker swarm init --advertise-addr <private-ip-address>; install Docker Engine on the worker machines and join each one using the token and address supplied by the manager. Deploy from the manager with docker stack deploy -c compose-swarm.yaml <stack-name>. section 9.1.1 section 9.1.2 section 9.1.3 section 9.1.4

Listing 9.5 Deploy blocks for Docker Swarm
version: '3.8' # legacy Docker Compose version
services:
  nginx:
    image: nginx
    ports:
      - 80:80
    volumes:
      - type: bind
        source: ./nginx.conf
        target: /etc/nginx/conf.d/default.conf
    deploy:
      replicas: 1
  web:
    image: codingforentrepreneurs/rk8s-py
    environment:
      - PORT=8080
    deploy:
      replicas: 5
  api:
    image: codingforentrepreneurs/cfe-nginx
    deploy:
      replicas: 2

See how each service gets its own deploy.replicas count in a Swarm-ready Compose file. section 9.1.2

Use docker stack ls to find deployed stacks and docker stack ps <stack-name> to inspect their tasks. After changing the configuration, run the same deploy command again; Swarm updates only changed services. Unlike docker compose up, a Compose run of this Swarm-ready file ignores its deploy.replica counts. section 9.1.4 section 9.1.2

How Road to Kubernetes covers it

How deep it goes
Works through configuration, cluster setup, deployment, and inspection commands; leaves GitOps workflows outside scope.
Examples
Uses NGINX to route requests to Python and Node.js applications, then adds replicas and worker nodes.
What it assumes
Builds on Docker Compose and virtual-machine access, while spelling out Docker Engine installation and cluster commands.
What you can do after
Operate/configure it by joining workers, deploying a stack, inspecting tasks, and redeploying changed services.
How it connects
Connects Docker Compose services and stacks to container orchestration, with Kubernetes and HashiCorp Nomad as alternatives.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.