Docker Swarm
Estimated reading: 5 minutes 283 views

Stacks, Services, and Tasks in Docker Swarm – Master Container Orchestration

Introduction – Why Learn Stacks, Services & Tasks in Docker Swarm?

Managing modern applications in production demands powerful orchestration. Docker Swarm delivers just that—by abstracting the complexity of container scheduling and service management into manageable units: Stacks, Services, and Tasks.

Whether you’re deploying a simple app or a full microservices architecture, understanding these core concepts will help you run scalable and resilient containerized applications with ease.

In this guide, you’ll learn:

  • What stacks, services, and tasks are in Docker Swarm
  • How they work together to orchestrate containers
  • Deployment examples with Docker CLI and Compose
  • Real-world analogies and visuals to make learning intuitive

Understanding Docker Swarm: Core Concepts


What is a Stack in Docker Swarm?

A Stack is a collection of interrelated services, networks, and volumes defined in a docker-compose.yml file. Think of it as a complete application package.

Real-World Analogy:

A Stack is like a web project folder:

  • /frontend – the UI service
  • /backend – the API logic
  • /database – database configuration

Example: docker-compose.yml File

version: "3.9"
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
  api:
    image: my-api:latest
    deploy:
      replicas: 2
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: example

Deploy the Stack:

docker stack deploy -c docker-compose.yml myapp

This command deploys 3 services (web, api, db) as part of the myapp stack.


What is a Service in Docker Swarm?

A Service is the definition of how a container behaves in the cluster:

  • What image to use
  • How many replicas to run
  • Which ports to expose
  • Placement rules and resources

Two Types of Services:

TypeDescriptionExample Use Case
ReplicatedRuns a specific number of containersWeb servers, APIs
GlobalRuns one container on each nodeLogging agents, exporters

Example: Create a Replicated Service

docker service create --name myweb --replicas 3 -p 8080:80 nginx:alpine

Breakdown:

  • --name myweb: Service name
  • --replicas 3: Run 3 containers
  • -p 8080:80: Map port 80 in container to 8080 on host

Docker Swarm spreads these replicas across nodes automatically.


What is a Task in Docker Swarm?

A Task is a running container instance created and managed by a service. It includes:

  • A specific container
  • Assigned node
  • Restart policies
  • Runtime status

View Tasks of a Service

docker service ps myweb

Example Output:

ID        NAME       IMAGE         NODE     DESIRED STATE  CURRENT STATE
x1ab12    myweb.1    nginx:alpine  node1    Running        Running 2 mins
x2ab13    myweb.2    nginx:alpine  node2    Running        Running 2 mins
x3ab14    myweb.3    nginx:alpine  node3    Running        Running 2 mins

How Stacks, Services, and Tasks Work Together

[ Stack: myapp ]
   ├── Service: web (1 replica) ──> Task A on node1
   ├── Service: api (2 replicas) ──> Task B on node2, Task C on node3
   └── Service: db (1 replica) ──> Task D on node1

Flow Summary:

  • Stack is the top-level deployment unit (e.g., myapp)
  • Service defines desired container behavior and configuration
  • Task is the actual container performing the work on a node

Benefits of Using Stacks, Services & Tasks in Docker Swarm

Feature Description
Stack ManagementDeploy multi-service apps with a single Compose file
Auto RecoveryFailed tasks are automatically restarted on healthy nodes
Load BalancingTraffic is evenly distributed across replicas
Declarative DeploymentDefine state with Compose; Swarm enforces it
Rolling UpdatesUpdate services without causing downtime
Security & RBACTLS encryption and access control for secure orchestration

Summary – Recap & Next Steps

Stacks, Services, and Tasks are the backbone of Docker Swarm orchestration. Mastering these components allows you to deploy, scale, and maintain applications confidently across clusters of machines.

Key Takeaways:

  • Stacks simplify complex multi-container deployments.
  • Services define the what and how many.
  • Tasks are the actual running containers doing the work.
  • Use Compose + Stack to manage everything declaratively.

Real-World Relevance: Whether building a small web app or a multi-tier enterprise system, Swarm’s abstractions help you focus on application logic instead of infrastructure plumbing.


Frequently Asked Questions (FAQ)

What’s the difference between a service and a task?

A service is the configuration (like a recipe), while a task is the actual running container (like the dish made from the recipe).


Can a task move between nodes?

No. Tasks are immutable once scheduled. If a node fails, the task is recreated on another node—not moved.


How do replicated and global services differ?

# Replicated (fixed number of tasks)
docker service create --replicas 3 nginx

# Global (one task per node)
docker service create --mode global prom/node-exporter

How can I check which tasks are running?

docker service ps <SERVICE_NAME>

This command shows each task’s state, assigned node, and image.


Why use stacks over individual service creation?

With one command:

docker stack deploy -c docker-compose.yml mystack

You can deploy all services, networks, and volumes together—perfect for managing complex applications.


Try It Yourself – Quick Hands-On Guide

Initialize Docker Swarm

docker swarm init

Create a docker-compose.yml file

Example:

version: "3.9"
services:
  frontend:
    image: nginx
  backend:
    image: node:18

Deploy the Stack

docker stack deploy -c docker-compose.yml myproject

Monitor Tasks

docker service ps <SERVICE_NAME>

Share Now :
Share

Stacks, Services, and Tasks in Docker Swarm

Or Copy Link

CONTENTS
Scroll to Top