To create a fully functional single-node Swarm cluster, we just type the following command:
$ docker swarm init
Swarm initialized: current node (jbl2cz9gkilvu5i6ahtxlkypa) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-470wlqyqbsxhk6gps0o9597izmsjx4xeht5cy3df5sc9nu5n6u-9vlvcxjv5jjrcps4trjcocaae 192.168.1.4:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
We call this process Swarm cluster initialization. This process initializes the new cluster by preparing the /var/lib/docker/swarm directory to store all states related to the cluster. Here's the contents of /var/lib/docker/swarm, which could be backed up if needed:
$ sudo ls -al /var/lib/docker/swarm
total 28
drwx------ 5 root root 4096 Sep 30 23:31 .
drwx--x--x 12 root root 4096 Sep 29 15:23 ..
drwxr-xr-x 2 root root 4096 Sep 30 23:31 certificates
-rw------- 1 root root 124 Sep 30 23:31 docker-state.json
drwx------ 4 root root 4096 Sep 30 23:31 raft
-rw------- 1 root root 67 Sep 30 23:31 state.json
drwxr-xr-x 2 root root 4096 Sep 30 23:31 worker
If we have many network interfaces on the host, the previous command will fail as Docker Swarm requires us to specify an advertised address using an IP address, or a certain network interface.
In the following example, I use my wlan0 IP address as the advertised address of the cluster. This means that any machine on the Wi-Fi network can try to join this cluster:
$ docker swarm init --advertise-addr=192.168.1.4:2377
Similarly, we may advertise using the name of a network interface, for example, eth0:
$ docker swarm init --advertise-addr=eth0
Choose the style that works best for your working environment.
After initialization, we get a fully working, single-node cluster. To force a node to leave the current cluster, we use the following command:
$ docker swarm leave --force
Node left the swarm.
If we run this command on a single-node cluster, the cluster will be destroyed. If you run the preceding command here, please do not forget to initialize the cluster again with docker swarm init before proceeding to the next section.