How to use docker node rm command to remove nodes from a swarm

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage nodes within a Docker Swarm by using the docker node rm command. You will begin by listing the existing nodes in a swarm to identify them. Then, you will attempt to remove a running node to understand the default behavior and limitations of the command.

Following that, you will explore how to successfully remove a stopped node from the swarm. Finally, you will learn how to forcibly remove an inaccessible node, which is crucial for handling scenarios where a node is no longer reachable. This hands-on experience will provide practical knowledge for maintaining your Docker Swarm cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") subgraph Lab Skills docker/ls -.-> lab-555185{{"How to use docker node rm command to remove nodes from a swarm"}} docker/ps -.-> lab-555185{{"How to use docker node rm command to remove nodes from a swarm"}} docker/rm -.-> lab-555185{{"How to use docker node rm command to remove nodes from a swarm"}} end

List existing nodes in the swarm

In this step, you will learn how to list the nodes that are currently part of a Docker Swarm. Before listing the nodes, we need to initialize a Docker Swarm.

First, initialize a new swarm on the current machine. This machine will become the manager node.

docker swarm init

You should see output indicating that the swarm has been initialized and providing a command to join other nodes as workers.

Now that the swarm is initialized, you can list the nodes in the swarm using the docker node ls command.

docker node ls

This command will display a table showing the nodes in the swarm. Initially, you will only see the manager node you just initialized. The output will include information such as the node ID, hostname, status, availability, manager status, and engine version.

For example, the output might look similar to this:

ID                            HOSTNAME            STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
<node_id> *                   <hostname>          Ready     Active         Leader           20.10.21

The * next to the ID indicates the current node you are on. The MANAGER STATUS column shows the role of the node in the swarm (e.g., Leader, Reachable, Unavailable).

Attempt to remove a running node from the swarm

In this step, you will attempt to remove a node that is currently running and part of the swarm. This will demonstrate that you cannot simply remove a running node without first taking it out of service.

First, let's list the nodes again to confirm the node ID of the running node.

docker node ls

Identify the ID of the node you want to attempt to remove. In this case, it will be the only node listed, which is the manager node.

Now, try to remove the node using the docker node rm command followed by the node ID. Replace <node_id> with the actual ID you found in the previous step.

docker node rm <node_id>

You will likely see an error message indicating that the node is a manager and cannot be removed this way, or that the node is still active. This is expected behavior. Docker Swarm prevents you from removing a running node directly to avoid disrupting services.

The error message might look something like:

Error response from daemon: node <node_id> is a swarm manager and cannot be removed without --force

This confirms that you cannot remove a running manager node without using the --force flag, which we will explore in a later step. For worker nodes, you would typically drain the node first before removing it.

Remove a stopped node from the swarm

In this step, you will learn how to remove a node from the swarm that is no longer running or accessible. Since we only have one node (the manager) in our current swarm, we will first simulate a scenario where a node is stopped by leaving the swarm on the current node.

First, leave the swarm on the current node. This will stop the Docker daemon from participating in the swarm.

docker swarm leave

You will be prompted to confirm that you want to leave the swarm. Type y and press Enter.

Node left the swarm.

Now, if you were on a different manager node in the same swarm, you could remove the node that just left. Since we are on the only node, we will re-initialize the swarm to simulate being on a different manager.

docker swarm init

You will see the output indicating the swarm is initialized again.

Now, let's list the nodes in the swarm. You will only see the current node listed.

docker node ls

To demonstrate removing a stopped node, we need to simulate having another node that has left the swarm. Since we cannot add another node in this environment, we will proceed to the next step which covers forcibly removing an inaccessible node, which is a more common scenario for removing nodes that are no longer participating in the swarm.

Forcibly remove an inaccessible node from the swarm

In this step, you will learn how to forcibly remove a node from the swarm that is inaccessible or unresponsive. This is necessary when a node has failed and cannot be gracefully removed.

First, let's list the nodes in the swarm again to get the node ID.

docker node ls

Identify the ID of the node you want to forcibly remove. In our current setup, this is the only node, which is the manager.

To forcibly remove a node, you use the docker node rm command with the --force flag, followed by the node ID. Replace <node_id> with the actual ID.

docker node rm --force <node_id>

You should see output indicating that the node has been removed.

Node <node_id> removed from swarm

Now, list the nodes in the swarm again to confirm that the node has been removed.

docker node ls

You will see an error message because you have removed the only node (the manager) from the swarm, and the current Docker daemon is no longer part of a swarm.

Error: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to a swarm.

This confirms that the node was successfully removed from the swarm, even though it was the manager node. Forcibly removing a manager node should be done with caution as it can impact the availability of your swarm if there are no other managers.

Summary

In this lab, you learned how to manage nodes within a Docker Swarm using the docker node rm command. You began by initializing a swarm and listing the existing nodes using docker node ls to identify their IDs and statuses.

Subsequently, you attempted to remove a running node, demonstrating that a node must be taken out of service before it can be removed. You then successfully removed a stopped node and learned how to forcibly remove an inaccessible node from the swarm, covering different scenarios for node removal.