How to use docker swarm init command to initialize a swarm

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to initialize a Docker Swarm using the docker swarm init command. You will start by initializing a basic swarm to understand the fundamental process.

Building upon the basic initialization, you will then explore more advanced scenarios. This includes initializing a swarm with a specific advertise address for controlling network communication, enabling autolock for enhanced security, and configuring a custom data path port. Through these steps, you will gain practical experience in setting up and customizing a Docker Swarm environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/SystemManagementGroup -.-> docker/info("Display System-Wide Information") subgraph Lab Skills docker/ls -.-> lab-555240{{"How to use docker swarm init command to initialize a swarm"}} docker/rm -.-> lab-555240{{"How to use docker swarm init command to initialize a swarm"}} docker/inspect -.-> lab-555240{{"How to use docker swarm init command to initialize a swarm"}} docker/info -.-> lab-555240{{"How to use docker swarm init command to initialize a swarm"}} end

Initialize a basic swarm

In this step, you will learn how to initialize a basic Docker Swarm. Docker Swarm is a native clustering and orchestration solution for Docker. It allows you to create and manage a cluster of Docker nodes.

Before initializing the swarm, let's check the current Docker status.

docker info

You should see information about your Docker installation. Look for the "Swarm" section. It should indicate that Swarm is inactive.

To initialize a basic swarm, you will use the docker swarm init command. This command turns the current Docker host into a swarm manager.

docker swarm init

After running the command, you will see output indicating that the swarm has been initialized and providing a command to join other nodes to the swarm as workers.

Let's verify that the swarm has been initialized successfully.

docker info

Now, the "Swarm" section should show that Swarm is active and indicate that the current node is a manager.

You can also use the docker node ls command to list the nodes in the swarm.

docker node ls

You should see the current node listed with the status "Ready" and the role "Manager".

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.

Initialize a swarm with autolock enabled

In this step, you will learn how to initialize a Docker Swarm with the autolock feature enabled. Autolock helps protect the swarm against unauthorized access by requiring a decryption key to unlock the swarm after a Docker daemon restart.

First, leave the existing swarm as you did in the previous step.

docker swarm leave --force

Now, initialize the swarm with the --autolock flag. You can also include the --advertise-addr flag again if you want to specify the advertise address. Replace <YOUR_IP_ADDRESS> with your machine's IP address.

docker swarm init --autolock --advertise-addr <YOUR_IP_ADDRESS>

When you initialize the swarm with --autolock, the output will include a "Swarm auto-lock key". It is crucial to save this key in a secure location. You will need it to unlock the swarm after a Docker daemon restart.

To verify that autolock is enabled, you can inspect the swarm.

docker swarm inspect --pretty

Look for the AutoLockManagers field in the output. It should be set to true.

Note that since this is a single-node swarm in a lab environment, you won't experience the full effect of autolock requiring a key after a daemon restart. However, this step demonstrates how to enable the feature.

Initialize a swarm with a custom data path port

In this step, you will learn how to initialize a Docker Swarm and specify a custom port for the data path network. The data path network is used for communication between swarm nodes for tasks like service discovery and load balancing. By default, this uses port 4789. You might need to change this port if it conflicts with other services on your network.

First, leave the existing swarm.

docker swarm leave --force

Now, initialize the swarm and specify a custom data path port using the --data-path-port flag. Let's use port 5789 as an example. You can also include the --advertise-addr flag and --autolock flag if you wish. Replace <YOUR_IP_ADDRESS> with your machine's IP address.

docker swarm init --data-path-port 5789 --advertise-addr < YOUR_IP_ADDRESS > --autolock

You will see the initialization output, including the autolock key if you enabled it.

To verify that the custom data path port is configured, you can inspect the swarm.

docker swarm inspect --pretty

Look for the DataPathPort field in the output. It should show the custom port you specified, which is 5789 in this example.

This concludes the steps on initializing a Docker Swarm with various options. You have learned how to initialize a basic swarm, specify an advertise address, enable autolock, and set a custom data path port.

Summary

In this lab, you learned the fundamental process of initializing a Docker Swarm. You began by initializing a basic swarm using the docker swarm init command, observing the changes in docker info and verifying the manager role with docker node ls.

Subsequently, you explored more advanced initialization options. You learned how to leave an existing swarm using docker swarm leave --force and then re-initialize the swarm while specifying a particular advertise address using the --advertise-addr flag. This demonstrated how to control the network interface used for swarm communication.