DEV Community

Cover image for Docker Swarm Use Cases: A Practical Guide Beyond Docker Compose
Naveed Ahmed
Naveed Ahmed

Posted on Originally published at blog.naveedkumbhar.com

Docker Swarm Use Cases: A Practical Guide Beyond Docker Compose

Docker Swarm Use Cases: When One Docker Host Is No Longer Enough

Docker Compose is perfectly reasonable for many applications.

You have a web service, an API, a database and perhaps a background worker. Everything fits comfortably on one server.

Then the requirements change.

The application needs to remain available during server maintenance. You want multiple replicas across different machines. Deployments should happen without manually connecting to every server.

Kubernetes is one option, but it isn't the only one.

Docker Swarm provides a smaller orchestration model that might already cover your requirements.

What Docker Swarm actually does

Swarm mode is built into Docker Engine.

Instead of managing containers individually on separate servers, you describe services and their desired configuration.

For example, you might ask Swarm to maintain three replicas of a web application.

If one task fails, the scheduler can create a replacement on an eligible node, assuming the cluster has enough capacity and an operational control plane.

Managers and workers

Swarm clusters have two node roles:

  • Managers maintain cluster state and coordinate scheduling.
  • Workers execute assigned tasks.

Managers can also run application workloads unless configured otherwise.

For production clusters, three managers are a common starting point.

Managers Required quorum Tolerated manager failures
1 1 0
3 2 1
5 3 2

Losing manager quorum does not automatically stop existing application containers. However, normal scheduling and management operations become unavailable.

Creating a small Swarm lab

Use disposable Linux virtual machines with Docker Engine installed.

On the first manager:

docker swarm init --advertise-addr MANAGER_IP
Enter fullscreen mode Exit fullscreen mode

Replace MANAGER_IP with a stable address reachable by the other nodes.

Display the worker join command:

docker swarm join-token worker
Enter fullscreen mode Exit fullscreen mode

Run the generated join command on the intended worker machines.

Keep join tokens private.

From the manager, check cluster membership:

docker node ls
Enter fullscreen mode Exit fullscreen mode

Deploying a replicated service

For a disposable learning environment, create an Nginx service:

docker service create \
  --name web \
  --replicas 3 \
  --publish published=8080,target=80 \
  nginx:alpine
Enter fullscreen mode Exit fullscreen mode

Inspect the service:

docker service ls
docker service ps web
Enter fullscreen mode Exit fullscreen mode

Swarm attempts to maintain the requested replica count.

For production releases, use tested image versions or immutable image digests instead of relying on floating tags.

Deploying a stack

If your application already uses Docker Compose, stack deployment may feel familiar.

Here is a small illustrative stack.yml:

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    networks:
      - frontend
    deploy:
      replicas: 3
      update_config:
        parallelism: 1
        delay: 10s
        failure_action: rollback
      restart_policy:
        condition: on-failure

networks:
  frontend:
    driver: overlay
Enter fullscreen mode Exit fullscreen mode

Use this as an alternative to the previous service example, not alongside another service already publishing the same port.

Deploy it from a manager:

docker stack deploy -c stack.yml demo
Enter fullscreen mode Exit fullscreen mode

Inspect the stack:

docker stack services demo
docker stack ps demo
Enter fullscreen mode Exit fullscreen mode

Unlike a normal Compose build workflow, stack deployment expects application images to be available to the cluster. Build and publish custom images before deploying them.

How containers communicate across hosts

Overlay networks let services communicate across different Docker nodes.

Applications can use service names instead of maintaining lists of container IP addresses.

Swarm also provides an ingress routing mesh. By default, a published service port can accept traffic through swarm nodes even when the destination task runs elsewhere.

For host-local routing requirements, Swarm also supports host-mode publishing.

Scaling and rolling updates

In a practice cluster:

docker service scale demo_web=5
Enter fullscreen mode Exit fullscreen mode

Swarm reconciles the service toward five replicas.

This is explicit scaling. Swarm does not automatically increase replicas in response to traffic without external automation.

Rolling updates can replace tasks in controlled groups. A failed update can trigger rollback when the policy is configured accordingly.

Remember that service rollback does not reverse database migrations or persistent application changes.

Managing secrets

Swarm can provide sensitive configuration to authorized services as mounted secret files.

On Linux, the usual location is:

/run/secrets/SECRET_NAME
Enter fullscreen mode Exit fullscreen mode

Applications need to support reading credentials from files.

Use Docker configs for non-sensitive configuration instead of treating them as secret storage.

Where Swarm makes sense

A few practical examples:

  • Small SaaS applications.
  • Internal reporting and administration tools.
  • Queue workers and scheduled processing services.
  • Edge and branch-office deployments.
  • Compose-based applications that need multi-host availability.

Stateful services require additional planning. A replacement container on another node may not have access to the original node's local volume.

When Kubernetes becomes a better fit

Swarm's smaller feature set becomes a limitation when your platform needs advanced autoscaling, custom controllers, stronger policy enforcement or several teams sharing infrastructure.

The decision should follow the workload rather than the popularity of the technology.

For a more complete walkthrough covering manager recovery, networking, secrets, stack deployment and production troubleshooting, read the Docker Swarm use cases guide.

Official reference: Docker Swarm mode documentation.

Top comments (0)