Jack Wallen shows you how to create a Docker server cluster, called Swarm, on Ubuntu Server 22.04.

Docker is my container deployment runtime. With it, I can quickly deploy containers to a network that can be easily accessed. What happens when I want to be able to scale those applications? Doing it on a single Docker server would be a challenge. To that end, it deploys multiple Docker instances and groups them into clusters. That, my friends, is called Docker Swarm.
SEE: Hiring Kit: Back-End Developer (TechRepublic Premium)
Let me show you how to implement a Docker Swarm on my de facto standard server distribution, Ubuntu. There are quite a few moving parts involved, so let’s get to it.
What you will need to implement a Docker Swarm
I am going to demonstrate with a three node cluster (one controller and two nodes). For that, you will need three instances of Ubuntu Server 22.04. You will also need a user with sudo privileges.
How to install the latest version of Docker
You’ll want to do this on every node in your Swarm.
First, add the Docker GPG key with the command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, add the official Docker repository:
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Next, we will install some dependencies with the command:
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
Finally, we can install the latest version of the Docker engine:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Finally, add your user to the docker group with the command:
sudo usermod -aG docker $USER
Please log out and log back in for the changes to take effect.
How to change the hostname on your servers
Let’s change the hostnames of your Docker Swarm servers. This will be done on all nodes. We’ll configure them as docker1, docker2, and docker3, but you can name them whatever you want.
To set the hostname, the command is:
sudo hostnamectl set-hostname HOSTNAME
Where HOSTNAME is the hostname of the machine.
How to map the hosts file
This is also done on each node. Open the hosts file with the command:
sudo nano /etc/hosts
At the bottom of the file, you’ll map each host like this (changing the entries to match their hostnames and IP addresses):
192.168.1.60 docker1
192.168.1.61 docker2
192.168.1.62 docker3
Save and close the file.
How to initialize the Swarm
Go to the controller node (docker1) and initialize swarm with the command, making sure to edit the IP address to match your needs:
docker swarm init --advertise-addr 192.168.1.60
You should then be presented with a join command that looks something like this:
docker swarm join --token SWMTKN-1-05rgkgq9hgvas7wfglzrumxymzxw3downs1afcbdr9kc7hq4cm-8ku8kxjsq57l1xnkl5lzjppro 192.168.1.60:2377
Run that command on each of your nodes. Once they have joined, you can check the swarm on the controller node with the command:
docker node ls
You should see something like this in the output:
tpsl7enzswhkeef3dh8uswkxp * docker1 Ready Active Leader 20.10.17
xnye548afhe1hc832kulh5sui docker2 Ready Active 20.10.17
cammaze2fcfcomjpdo0fwz105 docker3 Ready Active 20.10.17
Congratulations, your Docker Swarm is ready for your deployments. You can keep adding new nodes as needed for even more scaling and failover capacity.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube to get the latest tech tips for business professionals from Jack Wallen.