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.

LMMdocker-desktopmanager-2manager-3node-1node-2node-3

Part 1: Build the cluster

Three managers and three workers, all running on one Mac.

1Install Docker on your Mac

brew install --cask docker

Open Docker Desktop once so the engine is running.

2Initialize the Swarm

docker swarm init
Output of <code>docker swarm init</code> (join token and IP hidden)
Output of docker 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
The first node is the Leader
The first node is the Leader
Hostname
docker-desktop is 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:dind
docker run --privileged --name manager-3 -d docker:dind
Pulling <code>docker:dind</code> and starting both containers
Pulling docker:dind and starting both containers

Verify the containers:

docker ps
manager-2 and manager-3 are running
manager-2 and manager-3 are running

4Get the worker join token

On your Mac host, print the join command, or copy it from Step 2:

docker swarm join-token worker

The command has this shape. Replace the placeholders with your own values:

docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377

5Join 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>:2377
docker exec -it manager-3 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377
Both containers report “This node joined a swarm as a worker”
Both containers report “This node joined a swarm as a worker”

They join as workers because the worker token was used. Step 6 promotes them. Verify:

docker node ls
Two new nodes with a blank Manager Status (workers)
Two new nodes with a blank Manager Status (workers)

6Promote them to managers

docker node promote 2b044edc1459
docker node promote 8d2194a1591d
Promoting both nodes
Promoting both nodes

Verify the three-manager cluster:

docker node ls
One Leader and two Reachable managers
One Leader and two Reachable managers

7Create the worker node containers

docker run --privileged --name node-1 -d docker:dind
docker run --privileged --name node-2 -d docker:dind
docker run --privileged --name node-3 -d docker:dind
Creating node-1, node-2 and node-3
Creating node-1, node-2 and node-3

Verify the containers:

docker ps
All five containers are running
All five containers are running

8Join the containers to the swarm as workers

docker exec -it node-1 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377
docker exec -it node-2 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377
docker exec -it node-3 docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377
All three nodes join as workers
All three nodes join as workers

Verify. Nodes with a blank Manager Status are workers:

docker node ls
Three managers and three workers
Three managers and three workers

9Deploy 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;"'
The service converges with 3 of 3 tasks running
The service converges with 3 of 3 tasks running

Verify the service:

docker service ls
3/3 replicas, port 8081 published
3/3 replicas, port 8081 published

Open http://localhost:8081 in your browser:

The demo page served by the swarm
The demo page served by the swarm

Part 2: Monitor and test the cluster

Scale the service, drain nodes and watch Swarm reschedule tasks.

1Check images

docker images
The <code>docker:dind</code> and <code>nginx:alpine</code> images
The docker:dind and nginx:alpine images

2Check running containers

docker ps
All five containers are up
All five containers are up

3Check the three manager nodes

docker node ls --filter "role=manager"
Three managers, all Ready and Active
Three managers, all Ready and Active

All managers should show Ready and Active: one Leader and two Reachable.

4Scale out the service

docker service scale docker-web-app=6
Scaling to 6 replicas
Scaling to 6 replicas

Verify:

docker service ls
6/6 replicas running
6/6 replicas running

5Check how tasks are distributed

docker node ls
docker service ps docker-web-app
Node list. This capture was taken after the drain in step 6, so docker-desktop already shows Drain.
Node list. This capture was taken after the drain in step 6, so docker-desktop already shows Drain.
Tasks spread across workers and managers
Tasks spread across workers and managers

6Drain a manager to simulate maintenance

docker node update --availability drain docker-desktop

Or use the node ID: docker node update --availability drain <ID>

Draining docker-desktop
Draining docker-desktop

Verify the availability changed to Drain:

docker node ls
docker-desktop shows Drain
docker-desktop shows Drain

Verify the tasks:

docker service ps docker-web-app
Tasks on docker-desktop go to Shutdown and replacements start elsewhere
Tasks on docker-desktop go to Shutdown and replacements start elsewhere

7Bring the node back to Active

docker node update --availability active docker-desktop
Setting docker-desktop back to Active
Setting docker-desktop back to Active

Verify:

docker node ls
docker-desktop is Active again
docker-desktop is Active again

8Rebalance 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-app
Forced update converges at 6/6
Forced update converges at 6/6

Verify:

docker service ps docker-web-app
Old tasks shut down and new ones start
Old tasks shut down and new ones start

Or show only running tasks:

docker service ps docker-web-app --filter "desired-state=running"
Running tasks only
Running tasks only

9Drain the three worker nodes

Repeat the process, this time draining all three workers:

docker node update --availability drain 42c40d5c35cb
docker node update --availability drain 241b1aebb1dd
docker node update --availability drain cc80e6fa19ec
Draining the three workers
Draining the three workers

Verify:

docker node ls
All three workers show Drain
All three workers show Drain

10Check that all tasks run on the managers

docker service ps docker-web-app --filter "desired-state=running"
All running tasks are on manager nodes
All running tasks are on manager nodes

Match the NODE column with the hostnames in the node list:

docker node ls
Hostnames of the three active managers
Hostnames of the three active managers

Verify that the tasks are evenly distributed among the active managers (two tasks each):

docker service ps docker-web-app --filter "desired-state=running"
Two tasks on each active manager
Two tasks on each active manager