A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services).
swarm commands
docker swarm init
initialises a new docker swarm
docker swarm join –token
join a worker node to an existing swarm
docker swarm leave
leave the swarm
docker swarm init –autolock
enables autolock on the swarm
stack commands
docker stack deploy
used to deploy a new stack
docker stack ls
lists all the stacks and services that make up the stacks
docker stack services
list the services in a given stack
docker stack ps
list all the tasks for a given stack
docker stack rm
removes the stack
service commands
docker service create –replicas 5 -p 80:80 –name web nginx
creates a new docker service, based on nginx image with 5 replicas
docker service logs
outputs the logs of a service
docker service ls
lists all the services in the swarm
docker service rm
deletes a service
docker service scale
used to add or remove replicas from a service
docker service update
used to update a service with a new image
node commands
docker node ls
lists the docker nodes in a swarm
docker node ps
lists the tasks on a docker node
docker node rm
removes a node from the swarm
docker node demote
demotes a manager node
docker node promote
promotes a node to swarm manager
docker swarm network commands
docker network ls
lists networks
docker network create -d overlay network_name
create overlay network
docker network rm network_name
remove network
key concepts
Node
A physical or virtual machine on which docker is running
Manager
Performs swarm management and orchestration duties. Also acts as a worker node by default
Worker
Runs Docker Swarm tasks
Swarm Cluster
A group of Docker nodes working together
Stack
A collection of services that typically make up an application
Service
When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more).
Task
Services start tasks. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.
swarm features
cluster management
fully integrated with the docker engine. No additional software required for docker swarm mode
decentralized design
You can deploy both kinds of nodes, managers and workers, using the Docker Engine.
declarative service model
Docker Engine uses a declarative approach to let you define the desired state of the various services in your application stack.
scaling
For each service, you can declare the number of tasks you want to run.
desired state
The swarm manager node constantly monitors the cluster state and reconciles any differences.
multi-host networking
You can specify an overlay network for your services.
service discovery
Swarm manager nodes assign each service in the swarm a unique DNS name
load balancing
You can expose the ports for services to an external load balancer.
rolling updates
If anything goes wrong, you can roll back to a previous version of the service.
Hopefully you have found this docker swarm cheat sheet to be useful. If so, please share using the social links below! Also, be sure to check out our other articles covering Docker Certification objectives!