0
0
DockerHow-ToBeginner · 3 min read

How to Initialize Docker Swarm: Simple Steps to Start

To initialize Docker Swarm, run the command docker swarm init on your main Docker host. This sets up the current machine as the Swarm manager, enabling you to add worker nodes and manage your cluster.
📐

Syntax

The basic syntax to initialize Docker Swarm is:

  • docker swarm init: Starts a new swarm and makes the current node a manager.
  • --advertise-addr <IP>: Optional flag to specify the IP address other nodes use to connect.
  • --listen-addr <IP>: Optional flag to specify the address for swarm communication.
bash
docker swarm init [--advertise-addr <IP>] [--listen-addr <IP>]
💻

Example

This example shows how to initialize a Docker Swarm on your current machine with a specific IP address for other nodes to connect.

bash
docker swarm init --advertise-addr 192.168.1.100
Output
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-xyz 192.168.1.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
⚠️

Common Pitfalls

Common mistakes when initializing Docker Swarm include:

  • Not specifying --advertise-addr when the machine has multiple network interfaces, causing other nodes to fail connecting.
  • Running docker swarm init on a machine that is already part of another swarm.
  • Not having Docker installed or running before initializing the swarm.

Always check your network settings and Docker status before initializing.

bash
## Wrong: Initializing swarm without specifying advertise address on multi-interface machine

docker swarm init

## Right: Specify advertise address explicitly

docker swarm init --advertise-addr 10.0.0.5
📊

Quick Reference

Here is a quick summary of the docker swarm init command options:

OptionDescription
docker swarm initInitialize a new swarm and make this node a manager
--advertise-addr IP address for other nodes to connect to this manager
--listen-addr Address for swarm internal communication (default: 0.0.0.0:2377)
--force-new-clusterForce creation of a new swarm, even if this node was part of another swarm

Key Takeaways

Run docker swarm init to start a new swarm and make your machine the manager.
Use --advertise-addr to specify the IP address other nodes use to join the swarm.
Avoid initializing swarm on a node already part of another swarm without forcing a new cluster.
Ensure Docker is installed and running before initializing the swarm.
Check network interfaces to prevent connection issues between swarm nodes.