Show Menu
Cheatography

Docker Swarm Cheat Sheet by

docker swarm

Basic

Swarm is Docker’s in built containers orches­trator solution, its main purpose is to manage containers in a computer cluster, i.e. a set of connected computers that work together.
Swarm comes built into the Docker Engine, you don’t need to install anything to get started.
In Docker, there are many layers of encaps­ula­tion: the OS kernel, contai­ners, tasks that encaps­ulate containers as units of work, services or Pods that represent applic­ation compon­ents, and stacks­(co­lle­ction of services) that represent full applic­ations.

docker swarm commands

docker swarm init
Initialize a swarm
docker swarm join --toke­n<m­ana­ger­-to­ken> 10.1.0.2:2377
Join an existing swarm as manager node
docker swarm join --toke­n<w­ork­er-­tok­en> 10.1.0.2:2377
Join a swarm as a worker node
docker swarm leave
Leave the swarm

docker stack commands

docker stack deploy nodeapp -c docker­­-c­o­m­po­­se.yml
deploy the stack using docker­­-c­o­mpose file , Swarm does not support the build option if defined in the Compose file(but docker compose up uses it).
docker stack ls
shows all stacks along with list of services in the stack
docker stack services <st­ack­-na­me>
list the services in the stack.
docker stack ps <st­ack­-na­me>
list all the tasks in the stack.
docker stack rm <st­ack­-na­me>
removes the stack.
Manage Docker stacks( is a collection of services that make up an applic­ation in a specific enviro­nment).

docker service commands

docker service create --replicas 5 -p 80:80 --name web nginx
create docker service directly (similar to docker run command)
docker service logs [OPTIONS] SERVIC­E|TASK
Fetch the logs of a service or task, in option you can use -f,--d­etails etc
docker service ls
list all the services
docker service ps [OPTIONS] SERVICE [SERVI­CE...]
List the tasks of one or more services
docker service rm SERVICE [SERVI­CE...]
Remove one or more services
docker service scale SERVIC­E=R­EPLICAS [SERVI­CE=­REP­LIC­AS...]
Scale one or multiple replicated services
docker service update [OPTIONS] SERVICE
Update a service
Manage servic­es.S­er­vices in the swarm mode are actually object (when compared to the docker compose services where they are actual running services)

docker node commands

docker node ls
List nodes in the swarm
docker node ps
List tasks running on one or more nodes, defaults to current node
docker node rm [OPTIONS] NODE [NODE...]
Remove one or more nodes from the swarm
docker node demote NODE [NODE...]
Demote one or more nodes from manager in the swarm
docker node promote NODE [NODE...]
Promote one or more nodes to manager in the swarm
 

Common Terms/Key concepts

Node
is a physical or virtual machine (running an instance of the Docker Engine.)
Manager nodes
perform swarm management and orches­tration duties. By default manager nodes are also worker nodes.
Worker nodes
execute tasks.
Cluster
one or more nodes grouped together.
Swarm
is a type of cluster in docker termin­ology.
Docker Swarm
(not part of docker engine)is a separate product which you can use to cluster multiple Docker hosts. Prior to Docker version 1.12 it was the only native Docker option for clustering hosts, and it needed a lot of additional setup for distri­buted state, service discovery and security.
Swarm Mode
With Docker 1.12,(is built into Docker Engine) To run a cluster you just need to install Docker on multiple machines, run docker swarm init to switch to Swarm Mode and docker swarm join to add more nodes to the cluster. State, discovery and security are all included with zero setup.
Stack
is a collection of services that make up an applic­ation in a specific enviro­nment.
service
it defines the blueprint about which image to use and which commands to execut­e,p­ort­s,n­etw­ork­s,r­epl­ica­s,etc inside future running contai­ners.
task
in swarm model,task is actually invoked inside a contai­ner.When service is created the swarm manager starts a task(or its replicas) inside various contai­ners.

Swarm service diagram

Applic­ation

similar commands

Init host as a swarm manager node:
docker swarm init
Deploy application:
docker stack deploy -c docker-compose.yml myApp
List services:
docker service ls
docker stack services myApp
List tasks:
docker service ps myApp_web
docker container ls -q
docker stack ps myApp
Stop application:
docker stack rm myApp
Take down swarm
docker swarm leave --force
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Docker & Swarm 2022 Cheat Sheet

          More Cheat Sheets by gauravpandey44