Initialize a Docker Swarm
In this step, we will initialize a Docker Swarm. A Docker Swarm is a group of machines running Docker and joined into a cluster. After joining a Swarm, you can continue to run Docker commands you're used to, but they are now executed by a Swarm manager. The machines in a Swarm can be either managers or workers. Managers handle cluster management tasks, while workers execute the services.
Before initializing the swarm, let's check the current Docker version.
docker version
You should see output similar to this, indicating the Docker version installed on the LabEx VM:
Client: Docker Engine - Community
Version: 20.10.21
API version: 1.41
Go version: go1.16.20
Git commit: baeda1f
Built: Tue Oct 25 18:01:18 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.21
API version: 1.41 (minimum version 1.12)
Go version: go1.16.20
Git commit: 363bd3c
Built: Tue Oct 25 17:59:50 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.10
GitCommit: b4bd5d2b3d85c5e9b15588d67616e19a2a3a495d
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Now, let's initialize the Docker Swarm on this machine. Since this is the first node in the swarm, it will automatically become a manager node. We will use the docker swarm init
command.
docker swarm init
You should see output indicating that the swarm has been initialized and providing a command to join other nodes as workers. The output will look similar to this:
Swarm initialized: current node (xxxxxxxxxxxx) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The output confirms that the swarm is initialized and the current node is a manager. The xxxxxxxxxxxx
will be replaced by the actual node ID.