Initialize a Docker Swarm
In this step, you will initialize a Docker Swarm on your LabEx VM. A Docker Swarm is a cluster of Docker hosts that are running in swarm mode. Swarm mode enables you to manage a cluster of Docker nodes as a single virtual system.
Before initializing the swarm, let's check the current Docker version installed on the VM.
docker version
You should see output similar to this, indicating the Docker version is 20.10.21:
Client: Docker Engine - Community
Version: 20.10.21
API version: 1.41
Go version: go1.18.9
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.18.9
Git commit: 363bd3a
Built: Tue Oct 25 17:59:35 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.10
GitCommit: b4bd5d2bb63a5d10182b7e90689158e7c7b9b06b
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. When you initialize a swarm, the current node becomes the first manager node. Manager nodes handle swarm management tasks, such as maintaining the swarm state, scheduling services, and serving the swarm mode API.
Use the docker swarm init
command to initialize the swarm. We will specify the advertise address to ensure other nodes can join the swarm using the VM's IP address. Replace YOUR_VM_IP_ADDRESS
with the actual IP address of your LabEx VM. You can find this IP address in the LabEx environment details.
docker swarm init --advertise-addr YOUR_VM_IP_ADDRESS
After running the command, you will see output indicating that the swarm has been initialized and providing a command for other nodes to join the swarm as workers. Keep this join command handy, as you will need it in the next step.
Swarm initialized: current node (xxxxxxxxxxxx) is now a manager.
To add a worker to this swarm, run the following command on the worker node:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx YOUR_VM_IP_ADDRESS:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.