Skip to main content
In this comprehensive guide, you’ll learn how to move from single-host Docker Compose deployments to robust, multi-node orchestration using Docker Stack on a Swarm cluster. We’ll compare docker run vs. Compose, introduce Docker Swarm concepts, and walk through a real-world voting application example—complete with replicas, placement constraints, resource limits, and health checks.

1. Docker Run vs. Docker Compose

When you start with containers, you often use docker run for each service:
However, for multi-service applications, Docker Compose simplifies management by defining services in a single YAML file:
Launch all services together:
This approach centralizes configuration but is limited to a single host.

2. Introducing Docker Swarm and Stacks

Docker Swarm enables clustering multiple Docker engines into a single, fault-tolerant Swarm cluster. Instead of docker run, you create services:
With Docker Stack, you can use your Compose file to deploy across the Swarm:
Docker Stack uses the same Compose file format (v3+), so you can reuse your existing docker-compose.yml with minimal changes.

3. Containers, Services, and Stacks

  • Container: A running instance of an image, isolated with its dependencies.
  • Service: A scalable set of containers of the same image, distributed across Swarm nodes.
  • Stack: A collection of related services that define an application.
The image illustrates a hierarchical structure of a stack, showing the relationship between stacks, services, and containers using a pyramid and a diagram.

4. Example: Voting Application

We’ll deploy a simple voting app with Redis, PostgreSQL, vote service, result service, and a worker.

4.1 Single-Host Deployment with Docker Compose

All services run on your local Docker host.

4.2 Multi-Node Deployment with Docker Stack

Assume a Swarm with one manager and two workers. Enhance the Compose file with a deploy section for Swarm-specific settings.

4.2.1 Replicas

Scale services by defining replica counts:
Deploy the stack:

4.2.2 Placement Constraints

Ensure critical services run only on manager nodes:

4.2.3 Resource Limits

Protect node resources by setting CPU and memory limits:

4.2.4 Health Checks

Automatically monitor container health:

5. Common Stack Commands

Removing a stack stops and removes all associated services and containers. Use with caution in production environments.

References

Watch Video