Master Docker Swarm: Core Concepts, Modes, and Essential Commands
This guide explains Docker Swarm's built‑in cluster management, key concepts such as Swarm, Node, Stack, Service, Task, and load balancing, and provides step‑by‑step commands for initializing a Swarm, adding nodes, labeling, deploying stacks, and updating services.
Docker Swarm is Docker's built‑in container‑cluster management service, included since Docker 1.12.0 and also called Swarm Mode.
Compared with Kubernetes, Docker Swarm is simpler and fully compatible with docker‑compose, making it a good entry point for beginners.
Concepts
Docker Swarm consists of the following concepts:
Swarm
Node
Stack
Service
Task
Load balancing
Swarm refers to a cluster of computers linked by Docker. The docker swarm command creates, joins, or leaves a cluster.
A Node is a Docker host and can be a Manager or a Worker. At least one Manager is required; only Managers can run management commands, while both Managers and Workers can run Services.
A Stack is a group of Services, similar to docker‑compose. By default a Stack shares a single Network that isolates it from other Stacks.
A Service represents a set of containers. Services can run in replicated mode (a fixed number of containers) or global mode (one container on every eligible Node).
A Task is the smallest unit of work—a single container instance that executes a Service.
Load balancing in Swarm uses an Ingress network; accessing any node’s published port is automatically routed to the appropriate Service.
Modes
Replicated Mode
services:
some-service:
...
deploy:
mode: replicated
replicas: 3When mode is omitted, replicated with a single replica is the default.
Global Mode
services:
some-service:
...
deploy:
mode: global
placement:
...In global mode a container is scheduled on every eligible Node; placement constraints can limit which Nodes receive the container.
Common Operations
Create the First Node
docker swarm init --advertise-addr $IP$IP is the external IP of the node, allowing other nodes to join. After this command the Swarm has a single Manager.
Add a New Node
On a Manager, run:
docker swarm join-token manager
# copy the displayed command and run it on the new node:
docker swarm join --token SWMTKN-... <manager_ip>:2377
docker swarm join-token worker
# copy the displayed command and run it on the new node:
docker swarm join --token SWMTKN-... <manager_ip>:2377Running the join command on a machine that is not part of any Swarm adds it as a Manager or Worker.
Set Node Labels
docker node update $node_name --label-add main=trueLabels are key‑value pairs; they can be used in a Compose file’s placement.constraints to restrict services to nodes with a matching label.
Deploy a Stack
docker stack deploy $stack_name -c docker-compose.yaml -c other.yaml ...$stack_name is the name of the Stack. Multiple compose files can be supplied; the syntax is the same as with docker‑compose plus Swarm‑specific options.
Remove a Stack
docker stack rm $stack_nameUpdate a Service Image
docker service update --image $image:$tag $service_nameSigned-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.
