Docker Swarm is a container orchestration tool built into Docker that allows you to manage a cluster of Docker nodes as a single virtual system. It provides native clustering capabilities for Docker containers.
A service is the definition of the tasks to execute on the worker nodes.
A task is a running container that is part of a service.
Built-in load balancing distributes requests between containers.
# Initialize 600">swarm on manager 600">node
600">docker 600">swarm init --advertise-addr <MANAGER-IP>
# Example
600">docker 600">swarm init --advertise-addr 192.168.1.100Output:
Swarm initialized: current 600">node (x1x1x1x1x1x1) is now a manager.
To add a worker to this 600">swarm, run the following command:
600">docker 600">swarm join --token SWMTKN-1-0exampl3t0ken--192.168.1.100:2377
To add a manager to this 600">swarm, run 39;600">docker 600">swarm join-token manager39; and follow the instructions.# On worker 600">nodes, run the join command from above
600">docker 600">swarm join --token SWMTKN-1-0exampl3t0ken--192.168.1.100:2377
# To get the join token again
600">docker 600">swarm join-token worker# List 600">nodes in the 600">swarm
600">docker 600">node ls
# Inspect a specific 600">node
600">docker 600">node inspect <600">node-id>
# Promote a worker to manager
600">docker 600">node promote <600">node-id>
# Demote a manager to worker
600">docker 600">node demote <600">node-id>A service in Docker Swarm is a definition for running containers (tasks) across the cluster. You can scale services up or down, update them, and roll back if needed.
600">docker 600">service create --name webserver -p 80:80 600">nginxThis creates a service named webserver using the nginx image, exposing port 80.
600">docker 600">service scale webserver=3This scales the webserver service to 3 replicas.
600">docker 600">service update --image 600">nginx:600">alpine webserverThis updates the webserver service to use the nginx:alpine image.
Docker Swarm provides built-in networking for service discovery and communication between containers.
600">docker 600">network create -d overlay my_overlayThis creates an overlay network named my_overlay for multi-host communication.
600">docker 600">service create --name db --600">network my_overlay 600">postgresThis attaches the db service to the my_overlay network.
Swarm supports persistent storage using Docker volumes.
600">docker 600">volume create mydata600">docker 600">service create --name db --mount type=600">volume,source=mydata,target=/var/lib/600">postgresql/data 600">postgresSwarm automatically load balances requests to services across available nodes and containers.
600">curl http://<SWARM-IP>:80Requests will be distributed among the running containers for the service.
600">docker 600">swarm ca --rotateHere is a docker-compose.yml for deploying a simple web stack with Nginx and a static site:
"color:#F59E0B">version: '3.8'
"color:#F59E0B">services:
"color:#F59E0B">web:
"color:#F59E0B">image: nginx:alpine
"color:#F59E0B">ports:
- '80:80'
"color:#F59E0B">volumes:
- ./public:/usr/share/nginx/html:ro
"color:#F59E0B">deploy:
"color:#F59E0B">replicas: 2
"color:#F59E0B">restart_policy:
"color:#F59E0B">condition: on-failure
"color:#F59E0B">networks:
"color:#F59E0B">default:
"color:#F59E0B">external:
"color:#F59E0B">name: my_overlayDeploy the stack:
600">docker 600">stack deploy -c 600">docker-compose.yml my600">stack600">docker 600">stack 600">services my600">stack
600">docker 600">stack ps my600">stackDocker Swarm is a powerful, production-ready orchestrator built into Docker. While Kubernetes is more popular for large-scale deployments, Swarm is a great choice for simpler setups and teams already using Docker. With its easy setup, built-in security, and native Docker integration, Swarm remains a valuable tool for container orchestration.