9 of 10: Docker Swarm with Docker Containers.
Or: High Service Availability and Automated Load Balancing.

Thank you for reading this post.
My name is Brian and I'm a developer from New Zealand. I've been interested in computers since the early 1990s. My first language was QBASIC. (Things have changed since the days of MS-DOS.)
I am the managing director of a one-man startup called Digital Core (NZ) Limited. I have accepted the "12 Startups in 12 Months" challenge so that DigitalCore will have income-generating products by April 2024.
This blog will follow the "12 Startups" project during its design, development, and deployment, cover the Agile principles and the DevOps philosophy that is used by the "12 Startups" project, and delve into the world of AI, machine learning, deep learning, prompt engineering, and large language models.
I hope you enjoyed this post and, if you did, I encourage you to explore some others I've written. And remember: The best technologies bring people together.
Homelab | LXD Manager | Docker | Docker Desktop | Deno | MariaDB | Portainer | More Docker | Docker Swarm | CrowdSec
TL;DR.
I will install and set up Docker Swarm with a minimum of three Docker containers: one manager container and two worker containers. I will open some ports for communication, and deploy services using the docker stack commands.
An Introduction.
My previous post "in this 8-part mini-series" covered installing Portainer in a Docker container. My actual previous post, however, covered how to deploy multiple Docker containers. Why do I need multiple containers? Because a prerequisite for this lab is a minimum of three (3) Docker containers: I've named my containers Docker, Docker2, and (to absolutely no one's surprise) Docker3.
The purpose of this post is to show how to install Docker Swarm across, at least, three Docker containers.
The Big Picture.
The big idea is to set up an environment where I can develop my Docker Swarm management skills. From what I learned (online), Docker Swarm is easier to grasp than K3s or K8s. The big picture I'm looking toward combines Docker Swarm with Nginx reverse proxy and Nginx load balancing.
Setting Up a Docker Swarm.
Here are the steps I use to set up a Docker swarm:
I need at least 3 containers running Docker - The
Dockercontainer will run the Docker manager while theDocker2andDocker3containers will run the Docker workers. TheDockercontainer must be able to communicate with the other containers over the network.I install Docker within all of the containers.
From the terminal (
CTRL+ALT+T) that is connected to theDockercontainer, I initialize the swarm mode:sudo docker swarm init --advertise-addr 192.168.?.?
NOTE 1: This will make Docker (inside the Docker container) the host (or swarm manager).
NOTE 2: I replace the ? above with the IP address for the
Dockercontainer. This is the experimental part. If this does not work, then I will need to figure out how to reference the IP address of the Docker instance instead.
- I issue the following UFW commands in all the containers:
sudo ufw allow 2377/tcp
sudo ufw allow 2377/udp
sudo ufw allow 2376/tcp
sudo ufw allow 7946/tcp
sudo ufw allow 7946/udp
sudo ufw allow 4789/udp
2377 TCP for communication between manager nodes, 7946 TCP/UDP for node discovery, and 4789 UDP for overlay network traffic.
The other two containers can join the swarm as workers by running the command given in the output of the
docker swarm initcommand on theDockerterminal. It will be something like:sudo docker swarm join --token <token> <manager IP>:2377From the
Dockerterminal, I rundocker node lsto view the swarm:

- I can now deploy services to the swarm using
docker stack deploycommands. The services will be scheduled across the manager and worker nodes.
This covers the basic steps to setting up a Docker swarm.
The Results.
I demonstrated the process of setting up a Docker Swarm with Docker containers. By using a minimum of three containers, I was able to create a swarm of one manager node and two worker nodes. This setup allows for the efficient deployment and management of services across the swarm, providing a powerful tool for container orchestration. Docker Swarm will be a valuable addition to my toolkit as I continue to expand the capabilities of my homelab.
In Conclusion.
If you've been linearly following this blog, then you'll notice a higgledy-piggledy order to these articles. I've touched on a number of OpTec services and now I need a mind map to guide me through my own thoughts. I'm ready to search for an optimal layout for these servers. Something like a high availability, load-balanced version of the following might work as a starting point:

Until next time: Be safe, be kind, be awesome.
Homelab | LXD Manager | Docker | Docker Desktop | Deno | MariaDB | Portainer | More Docker | Docker Swarm | CrowdSec






