Initialize a swarm with a specific advertise address
In the previous step, you initialized a basic Docker Swarm. In this step, you will learn how to initialize a swarm and specify the address that the swarm manager will advertise to other nodes. This is useful in environments with multiple network interfaces or when you want to control which address is used for swarm communication.
First, you need to leave the existing swarm. You can do this using the docker swarm leave
command. Since this is the only node and it's a manager, you need to use the --force
flag.
docker swarm leave --force
You should see output confirming that the node has left the swarm.
Now, let's find the IP address of your current machine. You can use the ip addr show
command and look for the IP address associated with your network interface (commonly eth0
or ens*
).
ip addr show
Identify the IP address you want to use for the swarm's advertise address. For example, if your IP address is 172.17.0.2
, you will use that.
Now, initialize the swarm again, but this time specify the advertise address using the --advertise-addr
flag. Replace <YOUR_IP_ADDRESS>
with the actual IP address you found.
docker swarm init --advertise-addr <YOUR_IP_ADDRESS>
You will see output similar to the basic initialization, but the join command will now include the specified advertise address.
To verify that the swarm was initialized with the correct advertise address, you can inspect the swarm.
docker swarm inspect --pretty
Look for the AdvertiseAddr
field in the output. It should match the IP address you specified.