home
docker swarm
Container orchestration platform
print

docker version: 20.10+ - Date: June 2022

Cluster initialization

On the first Manager node

Initialize the cluster
docker swarm init --advertise-addr MANAGER-IP

Retrieve the token to add a new Manager node
docker swarm join-token manager

Retrieve the token to add a new Worker node
docker swarm join-token worker

On every new node

Join the node to the cluster
docker swarm join --token TOKEN

Cluster events

Show real time events docker events

Manage networks

Create a distributed overlay network docker network create --driver overlay NETWORK

Manage nodes

List all nodes
docker node ls

Inspect a node
docker node inspect NODE

List tasks running on a node docker node ps NODE

Promote a node to Manager
docker node promote NODE

Demote a node to Worker
docker node demote NODE

Update a node
docker node update [options] NODE

  • Add a label: docker node update --label-add type=front node-1
  • Drain a node: docker node update --availability drain node-1

Make a Worker node leave the Cluster
docker swarm leave

Remove a node from nodes list
docker node rm NODE


Manage services

Create a new service
docker service create [options] IMAGE [COMMAND] [ARGS...]

  • Create a busybox service: docker service create --name busybox busybox

List all services
docker service ls

List the tasks of a service
docker service ps SERVICE

Remove a service
docker service rm SERVICE

Update a service
docker service update [options] SERVICE

  • Update service image docker service update --image my-service:2 my-service

Scale a service
docker service scale SERVICE=REPLICAS

Inspect a service docker service inspect SERVICE

Fetch the logs of a service or task
docker service logs SERVICE docker service logs TASK

Roll back a service to its previous version
docker service rollback SERVICE

Manage stacks

Deploy a new stack from a compose file
docker stack deploy --compose-file COMPOSE-FILE STACK

List all stacks
docker stack ls

List the services of a stack
docker stack services STACK

List the tasks of a stack
docker stack ps STACK

Remove a stack
docker stack rm STACK

Manage secrets

Create a secret from a file or STDIN
docker secret create SECRET-NAME FILE-PATH echo STRING | docker secret create SECRET-NAME -

List all secrets
docker secret ls

Use a secret in a compose file

version: "3.1"
services:
  web:
    image: secrets_web
    secrets:
      - mysql-password
secrets:
  mysql-password:
    external: true