Cloud Native 10 min read

Master Docker Swarm and Portainer: Build, Visualize, and Scale a Container Cluster

This guide walks through installing Docker on Debian hosts, creating a Swarm cluster, exposing required ports, visualizing the cluster with Portainer, deploying sample services, scaling them, and implementing load balancing with Nginx, all illustrated with command snippets and screenshots.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Master Docker Swarm and Portainer: Build, Visualize, and Scale a Container Cluster

Environment

Install Docker on Debian 12 hosts using sudo apt install docker.io. Three hosts are prepared: a manager node (fs3) and two worker nodes (fs1, fs0) with IPs 192.168.1.95, 192.168.1.91, 192.168.1.92, all running Docker v20.10.24.

Open required ports: 2377/tcp for cluster management, 7946/tcp & 7946/udp for node communication, and 4789/udp for overlay networking.

Docker Swarm nodes
Docker Swarm nodes

Create Swarm Cluster

Initialize manager

On the manager node run:

sudo docker swarm init --advertise-addr 192.168.1.95

The command outputs the join token for workers.

Join workers

On each worker node execute:

sudo docker swarm join --token SWMTKN-... 192.168.1.95:2377

To join as a manager use docker swarm join-token manager --token .... Tokens expire after 24 hours; retrieve a new one with docker swarm join-token worker.

Verify the cluster with docker info, docker node ls, and view networks via docker network ls.

Cluster networks

Docker Swarm creates two default overlay networks: ingress (for service traffic) and docker_gwbridge (for inter‑node communication). List them with docker network ls.

Visualize with Portainer

Portainer provides a UI for managing the Swarm.

Single‑node deployment

Pull the image and run:

docker pull portainer/portainer-ce:latest
docker run -p 8000:8000 -p 9443:9443 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v portainer_data:/data \
  --name my-portainer -d --privileged=true \
  --restart=always portainer/portainer-ce:latest

Swarm deployment

Download the stack file:

curl -L https://downloads.portainer.io/ce2-19/portainer-agent-stack.yml -o portainer-agent-stack.yml

Modify the file to expose port 9001 on the agent service, then deploy:

docker stack deploy -c portainer-agent-stack.yml portainer

Portainer creates an overlay network portainer_agent_network and services portainer_agent and portainer. Access the UI at https://192.168.1.95:9443 and select “Docker Swarm” as the environment type.

Resetting the admin password

Stop the container, inspect to find the data volume, then run:

docker run --rm -v /var/lib/docker/volumes/portainer_data/_data:/data portainer/helper-reset-password

The command prints the new password.

Deploy a sample container service

Pull busybox, create an attachable overlay network, and launch a replicated service:

docker pull busybox
docker network create -d overlay --attachable busybox_overlay_network
docker service create -td --name busybox_service --network busybox_overlay_network --replicas=2 busybox

Portainer shows the two containers on different nodes with IPs 10.0.2.3 and 10.0.2.4, and they can ping each other.

Scale the service

Increase replicas to three via the Portainer UI.

Load balancing with Nginx

Deploy a stateless Nginx service with three replicas:

docker pull nginx
docker network create -d overlay --attachable nginx_overlay_network
docker service create -td --name nginx_service --network nginx_overlay_network --replicas=3 -p 8080:80 nginx

Each replica serves a custom index page showing its host IP, demonstrating round‑robin load balancing across the Swarm.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud Nativeload balancingOverlay Networkcontainer orchestrationDocker Swarmportainer
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.