Title: Docker Swarm Second Machine LAN Network Connection Refused
Introduction
Docker Swarm is a powerful orchestration tool for managing containers across multiple hosts. However, when attempting to connect the second machine to the Docker Swarm network, you may encounter the error "connection refused". This article will provide a detailed context and cover the key concepts to help you resolve this issue.
Prerequisites
Before diving into the solution, ensure that both machines are connected to the same LAN network and have the latest version of Docker installed.
Steps to Connect Second Machine to Docker Swarm
-
Initialize Docker Swarm on the Manager Node
On the manager node, run the following command to initialize the Docker Swarm:
docker swarm init --advertise-addr MANAGER_IPReplace
MANAGER_IPwith the IP address of the manager node. -
Join the Second Machine to the Docker Swarm
On the worker node, run the following command to join the Docker Swarm:
docker swarm join --token SWARM_TOKEN MANAGER_IP:2377Replace
SWARM_TOKENwith the token generated during the initialization step.
Common Issues and Solutions
-
Incorrect IP Address
Ensure that the
MANAGER_IPused in thedocker swarm initanddocker swarm joincommands is the correct IP address of the manager node. -
Firewall Configuration
Check if the firewall on the manager node is blocking the traffic from the worker node. Open ports 2375, 2376, and 2377 for TCP traffic on the manager node.
-
Docker Engine Configuration
Verify that the Docker engine on the worker node is configured to use the correct Docker Swarm manager. Update the
daemon.jsonfile on the worker node with the following configuration:{ "swarm": { "secrets": {}, "default-addr": "tcp://MANAGER_IP:2377", "listen-addr": "0.0.0.0:2377" } }Replace
MANAGER_IPwith the IP address of the manager node. -
Network Configuration
If the machines are connected to different subnets, you may need to configure the network settings on both machines to allow communication.
Code Example
Assuming you have initialized the Docker Swarm on the manager node with IP address 192.168.1.100 and have generated the token SWARM_TOKEN.
On the manager node:
docker swarm init --advertise-addr 192.168.1.100
On the worker node:
docker swarm join --token SWARM_TOKEN 192.168.1.100:2377
Summary
- Ensure both machines are connected to the same LAN network.
- Run the
docker swarm initcommand on the manager node and thedocker swarm joincommand on the worker node. - Check firewall, Docker engine, and network configurations on both machines.