Docker Compose — Docker Swarm

1 minute read

Docker Swarm is Docker’s built-in orchestration tool. If you know Compose, you already know most of Swarm — it uses the same compose.yml format with additional deploy options.

Swarm vs Kubernetes

Feature Docker Swarm Kubernetes
Learning curve Low (Compose-native) Steep
Setup complexity Simple (docker swarm init) Complex
Scaling Good Excellent
Community Smaller Massive
Cloud support Limited All major clouds
Best for Small-medium deployments Large-scale, enterprise

Swarm Basics

Initialize a swarm:

docker swarm init

Expected output:

Swarm initialized: current node (abc123def456) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-xxx-yyy 192.168.1.100:2377

Deploy a Stack

Swarm uses the same compose.yml with the docker stack command:

docker stack deploy -c compose.yml myapp
docker stack services myapp

Expected output:

ID             NAME           MODE         REPLICAS   IMAGE               PORTS
abc123def456   myapp_api      replicated   3/3        myapp-api:latest    *:3000->3000/tcp
def456abc123   myapp_db       replicated   1/1        postgres:16         

Compose Deploy Keys in Swarm

The deploy section in your compose.yml comes alive in Swarm:

services:
  api:
    image: myapp-api:latest
    deploy:
      replicas: 3
      update_config:
        parallelism: 1
        delay: 10s
        order: start-first
      rollback_config:
        parallelism: 1
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
      resources:
        limits:
          cpus: "0.5"
          memory: 256M

Rolling Updates

docker service update --image myapp-api:v2 myapp_api

Swarm performs a rolling update: replacing one container at a time, waiting 10 seconds between each (as configured above).

When to Choose Swarm

  • You need multi-host container orchestration but Kubernetes is too complex
  • Your team already knows Compose and wants the smoothest transition
  • Your deployment is small to medium scale (fewer than ~100 services)
  • You want built-in Docker tooling without third-party dependencies

When to Choose Kubernetes

  • You need auto-scaling, service mesh, or advanced networking
  • You are deploying to a managed cloud service (EKS, GKE, AKS)
  • Your organization has dedicated platform/infrastructure teams
  • You need the vast Kubernetes ecosystem of tools and integrations

Quick Reference

Item Detail
Initialize docker swarm init
Deploy docker stack deploy -c compose.yml myapp
List services docker stack services myapp
Scale docker service scale myapp_api=5
Update docker service update --image app:v2 myapp_api
Remove docker stack rm myapp