Decking out your home lab with useful (and downright wacky) self-hosted services never gets old, regardless of the number of years under your belt as a server owner. But once you transition from ad-riddled, subscription-based, data-collecting applications to a self-hosted service suite, you might want to set up load-balancing and auto-scaling provisions for your locally hosted toolkit. If you’re primarily a Docker user, the Docker Swarm utility provides a straightforward means to help you manage your containerized workloads – and here’s how you can set it up on your home lab.

What’s Docker Swarm?

And why should you use it?

Like Kubernetes, Docker Swarm is a container orchestration tool designed to help you set up clusters of PCs, SBCs, or even virtual machines for your containerization tasks. Thanks to Swarm, you can put together a reliable cluster of Docker hosts that can dynamically allocate (or remove) extra nodes to meet the resource needs of your containers. It also makes your home lab more fault-tolerant, as Docker Swarm can transfer the workloads of an offline node to the other systems in the cluster.

But unlike the typical clusters you’d find on Kubernetes or even virtualization platforms like Proxmox, Docker Swarm is a lot more accessible for beginners and home lab users. It’s also compatible with Portainer’s web UI, so you don’t have to deal with the hassle of learning new terminal commands to manage your arsenal of containers.

Setting up the Docker Swarm nodes

Technically, you could have a working Docker Swarm environment with just a manager and a worker node, but you won’t be able to use the cluster to its full potential. For a high-availability Docker Swarm setup, you’ll need three or more manager nodes (ideally an odd number) in the cluster. While I recommend using separate Linux-powered rigs to mitigate the issues with a single point of failure, you can also use virtual machines to configure your Docker cluster. Assuming you’ve already installed Docker Engine on each node,

  1. Boot your primary node and launch its terminal app.
  2. Run the ip addr command and note its IPv4 address:
    ip addr
  3. Execute this command to configure the node as the core of the Docker Swarm setup:
    sudo docker swarm init --advertise-addr IP_addr_of_manager
    Remember to replace the IP_addr_of_manager string with the IPv4 address you obtained in the previous step.
  4. Copy the token displayed in the terminal before switching to the other systems that you want to add to the server.
  5. Run the docker swarm join command followed by the token you've received from the manager node to add the other systems as worker nodes.
    sudo docker swarm join --token token_string IP_addr_of_manager:2377
    Make sure each node has a distinct name, otherwise, you may end up with issues where Docker fails to run services on a particular machine.
  6. (Optional) If you want to grant manager privileges to the worker nodes, you can switch to the main manager node and promote them with this command:
    sudo docker node promote name_of_worker_node

(Optional) Setting up Portainer for the Docker Swarm cluster

Although you can use terminal commands to manage your Swarm environment, Portainer is a solid tool for tinkerers who prefer the simplicity of a menu-based web UI. To configure Portainer on your Swarm cluster,

  1. Switch to the Manager node and use the curl command to grab the .yml file for Portainer.
    curl -L https://downloads.portainer.io/ce-lts/portainer-agent-stack.yml -o portainer-agent-stack.yml
  2. Use the docker stack deploy command to get the Portainer instance up and running:
    sudo docker stack deploy -c portainer-agent-stack.yml portainer
  3. You can access the Portainer UI by entering https://IP_addr_of_manager:9443 into the web browser of any system connected to the same network as the manager node.
  4. Choose a Password for the admin user and tap Create User.

Assuming you’ve performed all the steps correctly, the Portainer dashboard will appear, and the Swarm cluster will be accessible from the Home tab. Portainer has a beginner-friendly UI, so you shouldn’t encounter too many issues navigating its menu. But if you end up having a hard time with Portainer, we have a dedicated guide that can help you get accustomed to the container management platform.

Building a robust self-hosting cluster with Docker Swarm

With that, you’re free to tinker with the Swarm cluster to your heart’s content. For hardcore experimentation veterans and sysadmin/DevOps learners, it might be a good idea to branch out into Kubernetes as well. While it’s significantly more complex than Docker Swarm, K8s is compatible with Podman and a host of other professional tools. Not to mention, it has better scaling and auto-healing provisions than its Docker-based rival. But if you’re new to the home lab ecosystem or just want to self-host a couple of services, a Docker Swarm environment is the perfect solution to your container orchestration woes.

👁 Multiple containers running in Portainer
I use these 4 tools to enhance my Docker experience

A basic Docker installation is fine and all, but these four tools can elevate its functionality to the next level