This repository demonstrates different types of Docker Swarm deployments with a simple Node.js application. Docker Swarm is Docker's native clustering and orchestration solution that turns a group of Docker hosts into a single virtual Docker host.
- Overview
- Docker Swarm Architecture
- Example Scenarios
- Prerequisites
- Setup Instructions
- Testing
- Node.js Application
- Monitoring and Scaling
- Cleanup
This project demonstrates three common Docker Swarm scenarios:
- Basic Service Deployment - Single replicated service deployment
- Multi-Service Application - Frontend, backend, and database services working together
- Global Service Deployment - Services that run on every node in the swarm
┌─────────────────────────────────────────────────────────────┐
│ Docker Swarm Cluster │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Manager 1 │ │ Manager 2 │ │ Manager 3 │ │
│ │ (Leader) │ │ (Reachable) │ │ (Reachable) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └──────────────────┼───────────────────┘ │
│ │ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Worker 1 │ │ Worker 2 │ │ Worker 3 │ │
│ │ │ │ │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Docker Swarm consists of:
- Manager Nodes: Responsible for cluster management and orchestration
- Worker Nodes: Execute containers
- Services: Define the tasks to run on the swarm
- Tasks: Individual containers running on worker nodes
┌─────────────────────────┐
│ Node.js Web Service │
│ (Multiple Replicas) │
└─────────────────────────┘
A simple web service deployed with multiple replicas for high availability.
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ Frontend │ │ Backend API │ │ Database │
│ (3 replicas) │────▶│ (3 replicas) │────▶│ (1 replica) │
└───────────────┘ └───────────────┘ └───────────────┘
A typical three-tier application with:
- Frontend service (React/Node.js)
- Backend API service (Node.js)
- Database service (MongoDB)
┌─────────────────────────────────────────────────────────────┐
│ Docker Swarm Cluster │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Node 1 │ │ Node 2 │ │ Node 3 │ │
│ │ ┌──────────┐│ │ ┌──────────┐│ │ ┌──────────┐│ │
│ │ │Monitoring││ │ │Monitoring││ │ │Monitoring││ │
│ │ │Service ││ │ │Service ││ │ │Service ││ │
│ │ └──────────┘│ │ └──────────┘│ │ └──────────┘│ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
A monitoring service deployed globally to run on every node in the swarm.
- Docker Engine (version 19.03 or higher)
- Multiple machines for creating a true swarm (can use virtual machines)
- Basic understanding of Docker concepts
On the manager node:
# Initialize a new swarm (on the manager node)
docker swarm init --advertise-addr <MANAGER-IP>
# You'll get a command to run on worker nodes to join the swarm
# It will look like this:
# docker swarm join --token <TOKEN> <MANAGER-IP>:2377On each worker node:
# Run the join command that was output when initializing the swarm
docker swarm join --token <TOKEN> <MANAGER-IP>:2377Basic Service Deployment:
# Deploy the Node.js service with 3 replicas
docker service create --name nodejs-service \
--replicas 3 \
--publish 3000:3000 \
nodejs-app:latestMulti-Service Application:
# Create an overlay network for the application
docker network create --driver overlay app-network
# Deploy MongoDB service
docker service create --name mongo \
--network app-network \
--mount type=volume,source=mongo-data,destination=/data/db \
mongo:latest
# Deploy Backend API service
docker service create --name backend \
--network app-network \
--replicas 3 \
--env MONGO_URL=mongodb://mongo:27017/appdb \
nodejs-backend:latest
# Deploy Frontend service
docker service create --name frontend \
--network app-network \
--replicas 3 \
--publish 8080:80 \
--env API_URL=http://backend:3000 \
nodejs-frontend:latestGlobal Service Deployment:
# Deploy a monitoring service on every node
docker service create --name node-monitor \
--mode global \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
node-monitor:latestCreate a docker-compose.yml file for deploying services to a swarm.
# Deploy the stack using docker-compose.yml
docker stack deploy -c docker-compose.yml app-stackThis repository includes helper scripts to make it easier to set up and manage Docker Swarm examples.
The setup-swarm.sh script helps to initialize a Docker Swarm and deploy the examples:
# Make the script executable
chmod +x setup-swarm.sh
# Show usage information
./setup-swarm.sh --help
# Initialize a new swarm
./setup-swarm.sh init
# Deploy the basic service example
./setup-swarm.sh deploy-basic
# Deploy the multi-service application example
./setup-swarm.sh deploy-multi
# Deploy the global service example
./setup-swarm.sh deploy-global
# Remove all services and leave the swarm
./setup-swarm.sh destroyThe cleanup.sh script provides more granular control over cleaning up Docker resources:
# Make the script executable
chmod +x cleanup.sh
# Show usage information
./cleanup.sh --help
# Remove all services
./cleanup.sh services
# Remove all stacks
./cleanup.sh stacks
# Remove all overlay networks
./cleanup.sh networks
# Remove all volumes
./cleanup.sh volumes
# Remove all example images
./cleanup.sh images
# Stop and remove all containers
./cleanup.sh containers
# Leave the swarm
./cleanup.sh swarm
# Run all cleanup operations
./cleanup.sh all
# Use force flag to skip confirmation
./cleanup.sh --force allThe repository includes a test script to verify that the Docker Swarm examples are working correctly in a single-node environment.
The test-swarm.sh script can be used to test different deployment scenarios:
# Make the script executable
chmod +x test-swarm.sh
# Show usage information
./test-swarm.sh help
# Test the basic service deployment
./test-swarm.sh basic
# Test the multi-service application
./test-swarm.sh multi
# Test the global service deployment
./test-swarm.sh global
# Test all deployment scenarios sequentially
./test-swarm.sh allThe test script will:
- Check if Docker is running and if the machine is in swarm mode
- Initialize a swarm if necessary using local loopback address (127.0.0.1)
- Deploy the specified services
- Check if the services are healthy
- Make a test request to verify the service is accessible
- Clean up by removing the services
Note: The test script requires the
jqtool for JSON output formatting. Ifjqis not installed, the script will still work but JSON output won't be formatted.
The repository includes a simple Node.js application that can be used with these Docker Swarm examples. The application consists of:
- A basic HTTP server
- Health check endpoints
- Simple API functionality
See the app directory for source code and Dockerfile.
# List all services in the swarm
docker service ls
# View details of a specific service
docker service inspect --pretty <SERVICE-NAME>
# List all nodes in the swarm
docker node ls# Scale a service to 5 replicas
docker service scale <SERVICE-NAME>=5# View logs for a service
docker service logs <SERVICE-NAME>Using Docker CLI directly:
# Remove a service
docker service rm <SERVICE-NAME>
# Remove a stack
docker stack rm <STACK-NAME>
# Leave the swarm (on worker nodes)
docker swarm leave
# Force leave and remove node (on manager node)
docker node rm <NODE-ID>
# Leave swarm and remove all services (on manager node)
docker swarm leave --forceUsing the provided scripts:
# Using setup-swarm.sh
./setup-swarm.sh destroy
# Using cleanup.sh
./cleanup.sh allSee the Using the Helper Scripts section for more details on cleanup operations.