Skip to main content

Working with Docker Swarm Mode

1. Initializing Swarm

Command Description Notes
docker swarm init Initializes a new swarm Must be run on the manager node
docker swarm init --advertise-addr <ip> Initializes swarm with specific IP address Useful for multi-host environments
docker swarm init --listen-addr <ip> Specifies the address to listen on Defaults to 0.0.0.0:2377

Example: Initializing a Docker Swarm

docker swarm init
Note: The docker swarm init command returns a join token that is used to add worker nodes to the swarm.

2. Joining Swarm as Worker

Option Values Description
docker swarm join-token worker docker swarm join-token worker Generates the join token for worker nodes

Example: Joining a Worker Node

docker swarm join --token SWMTKN-1-xxx<REDACTED> xxx.xxx.xxx.xxx:2377
Note: The join token is valid for 24 hours by default. After that, a new token must be generated.

3. Joining Swarm as Manager

Option Values Description
docker swarm join-token manager docker swarm join-token manager Generates a token to join new managers to the swarm

Example: Joining a Manager Node

# On the manager node, generate a join token
docker swarm join-token manager

# Output will look like:
#   docker swarm join --token SWMTKN-1-xxx...yyy 192.168.1.10:2377

# On the new manager node, run:
docker swarm join --token SWMTKN-1-xxx...yyy 192.168.1.10:2377
Note: The join token is valid for 24 hours by default. Managers can be added to the swarm using the generated token.

4. Listing Swarm Nodes

Command Description Options Example
docker node ls List all nodes in the swarm --format, --filter docker node ls
docker node ls --format json Output in JSON format --format docker node ls --format json
docker node ls --filter role=manager Filter nodes by role --filter docker node ls --filter role=manager
Note: The docker node ls command lists all nodes in the swarm, including their status, availability, and roles.

5. Inspecting Nodes

Property Description Example
docker node ls List all nodes in the swarm docker node ls
docker node inspect <node-id> Inspect a specific node docker node inspect node-1
docker node ps <node-id> List tasks running on a specific node docker node ps node-1

Example: Inspecting a Node

docker node inspect node-1
Note: The output includes detailed information about the node's configuration, status, and resource usage.

6. Promoting Worker to Manager

Command Description Notes
docker node promote <node-id> Promotes a worker node to a manager node Requires swarm mode to be active

Example: Promoting a Worker Node

docker node promote worker-1
Note: The node must be in a healthy state to be promoted.
Warning: Promoting a node increases the number of managers in the swarm, which can impact performance and fault tolerance.

7. Demoting Manager to Worker

Command Description Notes
docker node demote <node-id> Demotes a manager node to a worker node Must be run from a manager node or with appropriate permissions

Example: Demoting a Manager Node

docker node demote manager-01
Note: Ensure that the cluster has sufficient worker nodes to maintain desired availability and redundancy after demotion.

8. Updating Swarm Configuration

Configuration Option Description Default Value Notes
dockerd --config-file Path to the JSON configuration file for Docker daemon /etc/docker/daemon.json Used to configure Docker daemon settings
--data-root Root directory for Docker data storage /var/lib/docker Must be accessible to all nodes in the swarm
--log-driver Default logging driver for containers json-file Can be overridden per container
--log-opts Options for the logging driver null Example: max-size=10m

Example: Updating Docker Daemon Configuration

{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "tcp://localhost:514",
    "syslog-facility": "local0"
  },
  "data-root": "/mnt/docker-data"
}
Note: Changes to the Docker daemon configuration require a restart of the Docker service on each node in the swarm.
Warning: Modifying data-root requires careful handling to ensure all nodes can access the new location.

9. Managing Join Tokens

Token Type Description Command Notes
manager Token for joining new manager nodes docker swarm join-token manager Used to securely add new manager nodes to the swarm
worker Token for joining new worker nodes docker swarm join-token worker Used to securely add new worker nodes to the swarm

Example: Generate a new join token for workers

docker swarm join-token worker

Example: Use a join token to add a worker node

docker swarm join --token SWMTKN-1-xxx worker-node-ip:2377
Note: Join tokens are valid for 12 hours by default. After that, they must be regenerated.

10. Removing Node from Swarm

Command Description Notes
docker node rm <node-id> Removes a node from the swarm Must be run on a manager node
docker node rm --force <node-id> Forcefully removes a node from the swarm Useful for unreachable nodes

Example: Removing a Node

# Remove a node from the swarm
docker node rm worker-01

# Force remove an unreachable node
docker node rm --force worker-01
Note: Before removing a node, ensure that its services are either replicated or stopped to avoid service disruption.

11. Leaving Swarm

Command Description Notes
docker swarm leave Leave the swarm Runs on worker nodes to leave the swarm. If node is a manager, use --force to leave without removing the swarm.
docker swarm leave --force Force leave the swarm Used to leave the swarm even if the node is a manager. This removes the node from the swarm and resets it to a standalone Docker daemon.

Example: Leaving a Swarm

docker swarm leave

Example: Force Leaving a Swarm

docker swarm leave --force
Note: After leaving the swarm, the node becomes a standalone Docker daemon and will no longer participate in swarm operations.

12. Configuring Swarm Encryption

Configuration Option Description Default Value Notes
--encryption-key Encryption key for swarm communication None Required for enabling encryption
--auto-encrypt Enable automatic encryption of swarm communication false Can be set to true to enable automatic encryption

Example: Enabling Swarm Encryption

docker swarm init --auto-encrypt
Note: The encryption key must be securely stored and managed to ensure the security of the swarm.
Warning: If encryption is enabled, all nodes in the swarm must have access to the encryption key.

Was this article helpful?