0
0
Dockerdevops~5 mins

Swarm mode initialization in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to run multiple Docker containers across several machines as one group, you use Swarm mode. Initializing Swarm mode sets up your machine as the manager that controls this group.
When you want to manage a cluster of Docker hosts as a single system.
When you need to deploy services that can scale across multiple servers.
When you want automatic load balancing between containers running on different machines.
When you want to easily add or remove machines from your container cluster.
When you want to ensure high availability by having multiple managers in your cluster.
Commands
This command starts Swarm mode on your current machine and makes it the manager. The --advertise-addr flag tells other machines how to reach this manager.
Terminal
docker swarm init --advertise-addr 192.168.1.100
Expected OutputExpected
Swarm initialized: current node (abcdef123456) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 192.168.1.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
--advertise-addr - Specifies the IP address or interface that other nodes use to connect to this manager.
This command lists all nodes in the Swarm cluster to verify the manager is active and see any joined nodes.
Terminal
docker node ls
Expected OutputExpected
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION abcdef123456 my-manager Ready Active Leader 20.10.12
Key Concept

If you remember nothing else from this pattern, remember: initializing Swarm mode turns your machine into the manager that controls your container cluster.

Common Mistakes
Not specifying the correct IP address with --advertise-addr.
Other machines cannot connect to the manager if the advertised address is wrong or unreachable.
Use the machine's real network IP address reachable by other nodes in the cluster.
Trying to initialize Swarm mode on a machine that is already part of another Swarm.
The command will fail because a node cannot be manager of two different Swarms at once.
Leave the existing Swarm first using 'docker swarm leave --force' before initializing a new one.
Summary
Run 'docker swarm init --advertise-addr <IP>' to start Swarm mode and make your machine the manager.
Use 'docker node ls' to check the status of nodes in your Swarm cluster.
The manager controls the cluster and other machines join it using the join token.