Docker Swarm on a Local Mac
Configure a Docker Swarm with three managers and three workers on one machine, using Docker-in-Docker containers. Then scale a service, drain nodes and watch Swarm reschedule tasks.
Part 1: Build the cluster
Three managers and three workers, all running on one Mac.
1Install Docker on your Mac
brew install --cask dockerOpen Docker Desktop once so the engine is running.
2Initialize the Swarm
docker swarm initdocker swarm init (join token and IP hidden)Docker prints a join command with a token. Use it to add more nodes. To add managers directly, run docker swarm join-token manager.
Verify the manager node:
docker node ls- Hostname
docker-desktopis the name of your Swarm manager (your local machine).- Status
- Ready and Active: the manager is healthy.
- Manager Status
- Leader: this node orchestrates the swarm, scheduling tasks and tracking the state and health of the other nodes.
3Create the manager-2 and manager-3 containers
Each container runs its own Docker engine (Docker-in-Docker), so it acts like a separate machine.
docker run --privileged --name manager-2 -d docker:dinddocker run --privileged --name manager-3 -d docker:dinddocker:dind and starting both containersVerify the containers:
docker ps4Get the worker join token
On your Mac host, print the join command, or copy it from Step 2:
docker swarm join-token workerThe command has this shape. Replace the placeholders with your own values:
docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:23775Join manager-2 and manager-3 to the swarm
Run the join command inside each new container:
docker exec -it manager-2 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377docker exec -it manager-3 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377They join as workers because the worker token was used. Step 6 promotes them. Verify:
docker node ls6Promote them to managers
docker node promote 2b044edc1459docker node promote 8d2194a1591dVerify the three-manager cluster:
docker node ls7Create the worker node containers
docker run --privileged --name node-1 -d docker:dinddocker run --privileged --name node-2 -d docker:dinddocker run --privileged --name node-3 -d docker:dindVerify the containers:
docker ps8Join the containers to the swarm as workers
docker exec -it node-1 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377docker exec -it node-2 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377docker exec -it node-3 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377Verify. Nodes with a blank Manager Status are workers:
docker node ls9Deploy docker-web-app as a service
Create a service with 3 replicas on port 8081. The command writes a custom page into each nginx container, so expand it to see the full command.
Full command with the demo page
docker service create \
--name docker-web-app \
--publish 8081:80 \
--replicas 3 \
nginx:alpine \
sh -c 'cat << "EOF" > /usr/share/nginx/html/index.html && nginx -g "daemon off;"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Docker Swarm Test – Amit Bandarkar</title>
<link href="https://fonts.googleapis.com/css2?family=Great+Vibes&family=Inter:wght@400;600&display=swap" rel="stylesheet">
<style>
:root{--bg:#f3f8ff;--card:#ffffff;--text:#0b2540;--muted:#5b7089;--accent:#0db7ed;--accent2:#384d54;--line:#d5e4f5;color-scheme:light;box-sizing:border-box;padding-top:env(safe-area-inset-top,0px);padding-bottom:env(safe-area-inset-bottom,0px)}
@media (prefers-color-scheme:dark){:root:not([data-theme="light"]){--bg:#07121f;--card:#0e1f33;--text:#e8f3ff;--muted:#8ea6bf;--accent:#2ec4f5;--accent2:#9fb7c4;--line:#1c3552;color-scheme:dark}}
:root[data-theme="dark"]{--bg:#07121f;--card:#0e1f33;--text:#e8f3ff;--muted:#8ea6bf;--accent:#2ec4f5;--accent2:#9fb7c4;--line:#1c3552;color-scheme:dark}
html{scroll-padding-top:env(safe-area-inset-top,0px)}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--text);font-family:Inter,system-ui,sans-serif;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px}
.wrap{max-width:760px;width:100%;text-align:center}
.badge{display:inline-block;padding:6px 14px;border:1px solid var(--line);border-radius:999px;color:var(--muted);font-size:.85rem;background:var(--card)}
h1{font-family:"Great Vibes","Brush Script MT",cursive;font-weight:400;font-size:clamp(3.2rem,12vw,6.5rem);margin:18px 0 4px;line-height:1.1;
background:linear-gradient(90deg,#0db7ed,#6a5cff,#ff4fa3);-webkit-background-clip:text;background-clip:text;color:transparent}
.sub{color:var(--muted);margin:0 0 28px;font-size:1.05rem}
.cluster{background:var(--card);border:1px solid var(--line);border-radius:18px;padding:22px}
.cluster h2{margin:0 0 14px;font-size:1rem;color:var(--accent2)}
.nodes{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:12px}
.node{border:1px solid var(--line);border-radius:12px;padding:12px}
.node b{display:block;font-size:.85rem;margin-bottom:8px}
.node small{color:var(--muted)}
.ctrs{display:flex;gap:6px;justify-content:center;margin-top:8px}
.c{width:22px;height:22px;border-radius:5px;background:var(--accent);animation:p 2s ease-in-out infinite}
.c:nth-child(2){animation-delay:.4s}.c:nth-child(3){animation-delay:.8s}
@keyframes p{50%{opacity:.35;transform:scale(.85)}}
.status{margin-top:20px;color:var(--muted);font-size:.9rem}
.dot{display:inline-block;width:9px;height:9px;border-radius:50%;background:#2ecc71;margin-right:6px}
code{background:var(--card);border:1px solid var(--line);padding:2px 8px;border-radius:6px}
</style>
</head>
<body>
<div class="wrap">
<span class="badge">🐳 Docker Swarm · Test Page</span>
<h1>Amit Bandarkar</h1>
<p class="sub">Hello from the swarm! If you see this, your service is up and running.</p>
<div class="cluster">
<h2>Example cluster · 3 managers</h2>
<div class="nodes">
<div class="node"><b>manager-1</b><small>Leader</small><div class="ctrs"><span class="c"></span></div></div>
<div class="node"><b>manager-2</b><small>Reachable</small><div class="ctrs"><span class="c"></span></div></div>
<div class="node"><b>manager-3</b><small>Reachable</small><div class="ctrs"><span class="c"></span></div></div>
</div>
</div>
<p class="status"><span class="dot"></span>Service status: <strong>running</strong> · Page loaded <span id="t"></span></p>
</div>
<script>document.getElementById("t").textContent=new Date().toLocaleString();</script>
</body>
</html>
EOF
'For a quick test, use this shorter version instead:
Simple one-line page
docker service create \
--name docker-web-app \
--publish 8081:80 \
--replicas 3 \
nginx:alpine \
sh -c 'echo "<h1>Welcome to My Personal Swarm Page!</h1><p>Running live on Docker Swarm across a multi-node cluster.</p>" > /usr/share/nginx/html/index.html && nginx -g "daemon off;"'Verify the service:
docker service lsOpen http://localhost:8081 in your browser:
Part 2: Monitor and test the cluster
Scale the service, drain nodes and watch Swarm reschedule tasks.
1Check images
docker imagesdocker:dind and nginx:alpine images2Check running containers
docker ps3Check the three manager nodes
docker node ls --filter "role=manager"All managers should show Ready and Active: one Leader and two Reachable.
4Scale out the service
docker service scale docker-web-app=6Verify:
docker service ls5Check how tasks are distributed
docker node lsdocker service ps docker-web-app6Drain a manager to simulate maintenance
docker node update --availability drain docker-desktopOr use the node ID: docker node update --availability drain <ID>
Verify the availability changed to Drain:
docker node lsVerify the tasks:
docker service ps docker-web-app7Bring the node back to Active
docker node update --availability active docker-desktopVerify:
docker node ls8Rebalance the cluster
Swarm does not move running tasks back on its own. Force a rolling update to spread them evenly across all nodes:
docker service update --force docker-web-appVerify:
docker service ps docker-web-appOr show only running tasks:
docker service ps docker-web-app --filter "desired-state=running"9Drain the three worker nodes
Repeat the process, this time draining all three workers:
docker node update --availability drain 42c40d5c35cbdocker node update --availability drain 241b1aebb1dddocker node update --availability drain cc80e6fa19ecVerify:
docker node ls10Check that all tasks run on the managers
docker service ps docker-web-app --filter "desired-state=running"Match the NODE column with the hostnames in the node list:
docker node lsVerify that the tasks are evenly distributed among the active managers (two tasks each):
docker service ps docker-web-app --filter "desired-state=running"