How to Build a Docker Swarm Cluster on Three Ubuntu Nodes Step‑by‑Step
This guide walks you through setting up a Docker Swarm cluster on three Ubuntu 16.04 servers, covering environment preparation, Docker installation, configuring a Consul discovery backend, creating manager and worker nodes, and managing containers across the swarm using the Docker remote API.
Setup Base Environment
Create three virtual machines (or physical hosts) and install Ubuntu 16.04 Server amd64 . Enable SSH for remote access, install Docker, optionally set up a private Docker Registry, and enable root login.
Configure Nodes
All subsequent operations are performed as the root user on each node (manager0, node0, node1).
Upgrade system packages:
apt-get update && apt-get -y upgrade && apt-get -y install curlInstall Docker Engine: curl -sSL https://get.docker.com/ | sh Start Docker Engine listening on port 2375 (required for Swarm communication):
docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sockConfigure Consul Discovery Backend
On the manager node, pull the Consul image and start it as a bootstrap server:
docker pull progrium/consul docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrapCreate Swarm Cluster
Create the manager (Swarm control plane) and bind it to the Consul service:
docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise 192.168.2.56:4000 consul://192.168.2.56:8500Note: manager0 also runs the Consul backend, so the advertise IP is the same.
Join the worker nodes to the cluster:
# node0 (IP 192.168.2.77)
docker run -d swarm join --advertise 192.168.2.77:2375 consul://192.168.2.56:8500 # node1 (IP 192.168.2.129)
docker run -d swarm join --advertise 192.168.2.129:2375 consul://192.168.2.56:8500Verify the cluster status:
docker -H :4000 infoUse the Cluster
Run a container on the swarm via the manager: docker -H :4000 run hello-world Repeated runs distribute containers across the two worker nodes, balancing the load.
Common management commands (note the -H :4000 flag to target the swarm):
# List containers on the cluster
docker -H :4000 ps # Remove a container
docker -H :4000 rm -f <em>ContainerID</em> # Start / stop / restart a container
docker -H :4000 start <em>ContainerID</em>
docker -H :4000 stop <em>ContainerID</em>
docker -H :4000 restart <em>ContainerID</em>The only difference from direct Docker commands is the addition of -H :4000 to point to the Swarm manager.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
