A swarm is initialized by using the command swarm init
. The node on which the command is executed will play the role of a manager.
$ docker swarm init [options]
After creating the swarm, the above command displays a note indicating the commands to run to join a worker or a manager to the newly created swarm.
If the node has multiple ip addresses, the option --advertise-addr has to be specified with the ip address to use for inter-manager communications and overlay network.
$ docker swarm init --advertise-addr 192.168.1.2:2377
To verify that the swarm was correctly initiated:
$ docker info
To join a node to a swarm, the join command has to be run on the node. A token or a secret is used to prevent adding nodes without authorization.
A node can join a swarm as worker or as manager depending on the token passed to the join command.
To show the commands to use to join nodes, run the following command while specifying the role of the node, either manager or worker, as argument:
$ docker swarm join-token manager
$ docker swarm join-token worker
An example of the generated command is:
$ docker swarm join --token SWMTKN-1-4bx3fl9pp98vwcp91jpeecj4h9ephager4j28qxwttzpcjm940-85h5stswtrfzrddjc6oxxagyr 192.168.65.3:2377
$ docker swarm leave [options]
The leave command allows the disconnection of nodes from a swarm.
Running the command on a worker will remove the worker from the swarm.
When there are multiple managers in a swarm, a manager has to be demoted to a worker first before removing it from the swarm.
$ docker node demote <node-name>
$ docker swarm leave
For single-node swarm with one manager, the option --force (-f) can be used to stop the swarm.
$ docker swarm leave -f