Docker Certified Associate Exam Course

Docker Swarm

Swarm Architecture

Running containers on a single Docker host is convenient for development or testing, but in production it introduces a single point of failure. If that host goes down, all your services become unavailable. Docker Swarm solves this by clustering multiple Docker hosts into one logical unit, providing high availability, load balancing, and seamless scaling.

Swarm Cluster Components

A Swarm cluster groups physical or virtual machines—on-premises or in the cloud—into a unified environment. Every node runs Docker Engine and joins the cluster either as a manager or a worker.

Node TypeResponsibilitiesCommands
ManagerMaintains desired state, schedules tasks, serves the APIdocker node ls<br/>docker node promote
WorkerExecutes tasks assigned by managers, runs service containersdocker node ls<br/>docker node demote

Note

By default, manager nodes can handle workloads in addition to management tasks. To dedicate a manager solely to orchestration, use docker node update --availability drain <node>.

When you deploy an application, you submit a service definition to a manager. The manager translates it into tasks and distributes them across worker nodes, which then run the required containers.

Declarative Service Definitions

Docker Swarm uses declarative YAML files—similar to Docker Compose—to define multi-service applications. Store these files in version control to track changes and facilitate CI/CD workflows:

# service-definition.yml
version: "3.8"
services:
  web:
    image: "simple-webapp:latest"
    ports:
      - "80:80"
  database:
    image: "mongo:5.0"
    volumes:
      - db-data:/data/db
  cache:
    image: "redis:alpine"
    deploy:
      replicas: 2

volumes:
  db-data:

Note

Declarative definitions allow you to scale, update, and rollback services with a single command: docker stack deploy -c service-definition.yml my_stack.

Key Features of Docker Swarm

1. Simplified Setup and Maintenance

Swarm is built directly into Docker Engine, so there’s no extra software to install. With the Docker CLI you can:

  • Initialize a new cluster: docker swarm init
  • Join nodes to the cluster: docker swarm join --token <token> <manager-ip>:2377
  • Promote or demote managers: docker node promote <node>

The image lists features of Docker Swarm, such as simplified setup and scaling, alongside a diagram showing a Docker Swarm setup with manager and worker nodes.

2. Scalability and Load Balancing

You can scale services on demand using:

docker service scale web=10

Swarm’s internal load balancer distributes requests across all healthy containers. If you need an external load balancer, point it at any manager or worker node.

The image illustrates the features of Docker Swarm, highlighting aspects like simplified setup, scaling, and load balancing, alongside a diagram showing an external load balancer connected to Docker hosts with web services.

3. Rolling Updates and Self-Healing

Swarm performs rolling updates by default, updating one container at a time:

docker service update \
  --image simple-webapp:2.0 \
  --update-parallelism 2 \
  --update-delay 10s \
  web

If a container crashes or fails health checks, Swarm automatically replaces it to match the desired state.

Warning

Always test updates in a staging environment before applying to production. Use --rollback to revert quickly if an update misbehaves.

4. Secure Networking and Service Discovery

Node-to-node communication is secured with mutual TLS. Overlay networks let containers on different hosts communicate as if they were on the same LAN. Built-in DNS routing ensures each service name resolves to the correct VIP or container IP.

The image describes features of Docker Swarm, including simplified setup, scaling, and service discovery, alongside a diagram showing a manager node with a DNS server and worker nodes with web services.

Summary and Next Steps

In this article, we covered the core architecture and features that make Docker Swarm a powerful container orchestration solution. You learned about:

  • Cluster components and node roles
  • Declarative service definitions
  • Key features: setup, scaling, updates, and networking

Next, dive into practical guides on:

  • Setting up a multi-node Swarm cluster
  • Deploying production-grade services
  • Advanced networking patterns

Watch Video

Watch video content

Previous
Container Orchestration Introduction