Deploy a Docker Swarm on Rocky Linux

You may have heard recently that Red Hat has, in my opinion, gone against the heart and soul of open source. If you’ve not heard, essentially it is putting up a paywall such that every clone of RHEL cannot access the source without paying up. Because of that, many users are migrating to alternatives, such as Rocky Linux.
But as with RHEL, Rocky Linux defaults to Podman as its default container runtime engine. If you’ve been using Docker for years, Podman is a good option but it’s not exactly a 1:1 migration. And so, if Docker is your preferred container deployment tool, you’ll be happy to know that it’s possible to not only install Docker on Rocky Linux but to also deploy a full-blown Docker Swarm on the platform. Even better, you can install Docker on Rocky Linux without removing Podman, so you can enjoy the best of both worlds.
I’m going to show you how to do exactly that. Once complete, you’ll feel right at home deploying and managing Docker containers on this Red Hat Enterprise Linux clone that continues to honor the spirit of open source.
What You’ll Need
To deploy a Docker Swarm on Rocky Linux you’ll need at least two running instances of the open source operating system and a user with sudo privileges. That’s it. Let’s make this happen.
Install the Necessary Dependency
The first thing we’re going to do is install the necessary dependency. This is done on all instances of Rocky Linux. Log into the first instance and run the command for the first dependencies like so:
1 |
sudo dnf install dnf-utils -y |
When that installation completes, you’re ready to move on to the Docker installation.
Install Docker
Again, this is done on all instances of Rocky Linux.
Add the required repository with the command:
1 |
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo |
Once you’ve added the repository, you can then run the command to install everything necessary for Docker Swarm. That command is:
1 |
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y |
With Docker installed, you’ll want to start and enable the service with the command:
1 |
sudo systemctl enable --now docker |
Finally, add your user to the docker group with:
1 |
sudo usermod -aG docker $USER |
Log out and log back in for the changes to take effect.
Open the Firewall
Before we deploy Docker Swarm, we must first open a few ports in the firewall, which is achieved with the following commands (run on all instances of Rocky Linux):
1 2 3 4 |
sudo firewall-cmd --add-port=2377/tcp --permanent sudo firewall-cmd --add-port=7946/tcp --permanent sudo firewall-cmd --add-port=7946/udp --permanent sudo firewall-cmd --add-port=4789/udp --permanent |
Reload the firewall with the command:
1 |
sudo firewall-cmd --reload |
Initialize the Swarm
With all of that out of the way, we can now initialize the Swarm. This is done on the machine that will serve as your controller. You will be required to know the IP address of your Swarm manager before you run this command. For example’s sake, let’s say the IP address of our Swarm manager is 192.168.1.192. Go back to that machine and issue the command:
1 |
docker swarm init --advertise-addr 192.168.1.192 |
The output of the above command will include the command you must run on all Docker nodes that will join the Swarm. That command will look like this:
1 |
docker swarm join --token SWMTKN-1-42cpopou4iljvubx00z53uvj2oc9muqtjucbryrnw97smnwcwm-e4mp25qupifa19xuxzlg6ic6u 192.168.1.192:2377 |
Of course, the random string of characters and IP address will be different for your instance.
Copy that command and run it on the first node that will join your Swarm.
Upon successfully joining the Swarm, the output of the command will include:
This node joined a swarm as a worker.
Testing the Node
You can verify the connection by going back to the controller and issuing the command:
1 |
docker info |
Within the output of the command, you should see a section that looks something like this:
1 2 3 4 5 6 |
Swarm: active NodeID: iy5c7w7s6zgd8vsr22gnrl1uz Is Manager: true ClusterID: qkkabtj3db6niknzgwyph2l98 Managers: 1 Nodes: 2 |
As you can see, the swarm is active and there are currently two nodes attached. You can then add more nodes by first running the init command again, copying the command in the output, and running it on the next node. Every time you add a new node, you must run the init command to get a unique join command for the new node.
Another simple test is to deploy a service to the new Docker Swarm. Let’s deploy a simple NGINX service with the command:
1 |
docker service create --name nginx_test nginx |
That command should successfully deploy an NGINX service to a single node. If you want to deploy the service to 2 nodes, the command would be:
1 |
docker service create --replicas 2 --name nginx_test nginx |
For 3 nodes, the command would be something like this:
1 |
docker service create --replicas 3 --name nginx_test nginx |
If you’ve already deployed the single node service, you can scale it to however many nodes you need with a simple command. Let’s say you have five nodes joined to your Swarm. To scale that service to five, the command would be:
1 |
docker service scale nginx_test=5 |
Congratulations, you’ve just deployed your first Docker Swarm on Rocky Linux.