Introduction
In this lab, you will learn how to use the docker node inspect command to view detailed information about Docker Swarm nodes. We will start by inspecting a single node using its ID, then explore how to inspect the current node using the 'self' keyword. Finally, you will learn how to format the output to display specific information and how to pretty-print the node details for better readability. This hands-on experience will equip you with the skills to effectively examine and understand the configuration and status of your Docker Swarm nodes.
Inspect a single node
In this step, we will learn how to inspect a single Docker node. The docker node inspect command provides detailed information about a specific node in a Docker Swarm.
First, let's check the version of Docker installed on the LabEx VM.
docker version
You should see output similar to this, indicating the Docker version is 20.10.21:
Client: Docker Engine - Community
Version: 20.10.21
API version: 1.41
Go version: go1.18.9
Git commit: baedd2f
Built: Tue Oct 25 17:58:10 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.21
API version: 1.41 (minimum version 1.12)
Go version: go1.18.9
Git commit: 305620d
Built: Tue Oct 25 17:56:04 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.10
GitCommit: b35cd9b83c7c7b1200458f9f9f8d73288d61e58e
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Now, let's initialize a Docker Swarm. This is necessary to have nodes to inspect.
docker swarm init
You should see output indicating the swarm has been initialized and providing a join token:
Swarm initialized: current node (xxxxxxxxxxxx) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The output shows that the current node is now a manager. The xxxxxxxxxxxx is the ID of the current node. We will use this ID to inspect the node.
Now, let's inspect the current node using its ID. Replace YOUR_NODE_ID with the actual node ID from the docker swarm init output.
docker node inspect YOUR_NODE_ID
For example, if your node ID was abcdef123456, the command would be:
docker node inspect abcdef123456
This command will output a large JSON object containing detailed information about the node, including its status, role, hostname, and more.
Inspect the current node using 'self'
In the previous step, we inspected a node using its specific ID. Docker Swarm provides a convenient alias, self, to refer to the current node. This is useful when you want to inspect the node you are currently operating on without needing to know its ID.
Let's use the self alias to inspect the current node.
docker node inspect self
This command will produce the same detailed JSON output as inspecting the node by its ID. Using self makes your commands more portable and easier to use, especially in scripts or when you don't have the node ID readily available.
The output will be a large JSON object, similar to what you saw in the previous step. This JSON contains all the configuration and status information for the current node.
Format the output to show specific information
In the previous steps, we saw that docker node inspect outputs a large JSON object. Often, you only need specific pieces of information from this output. Docker provides the --format flag to extract and format specific data using Go's text/template package.
Let's use the --format flag to display only the hostname of the current node. The hostname is located at the .Description.Hostname path within the JSON output.
docker node inspect --format '{{ .Description.Hostname }}' self
This command will output just the hostname of the current node. The {{ .Description.Hostname }} part is the Go template syntax to access the Hostname field within the Description object of the JSON output.
Now, let's try to display the node ID and the node availability. The node ID is at the .ID path, and the availability is at the .Spec.Availability path. We can combine multiple fields in the format string.
docker node inspect --format 'ID: {{ .ID }}, Availability: {{ .Spec.Availability }}' self
This command will output the node ID and its availability in a more readable format. You can use this technique to extract any field from the JSON output of docker node inspect.
Pretty-print the node information
In the previous steps, we saw the raw JSON output and how to extract specific fields. Sometimes, you want a more human-readable, structured output of the node information without having to parse the raw JSON yourself. The docker node inspect command provides a --pretty flag for this purpose.
Let's use the --pretty flag to display the node information in a more readable format.
docker node inspect --pretty self
This command will output a formatted summary of the node's information, including its ID, hostname, status, availability, and role. This output is much easier to read and understand at a glance compared to the raw JSON.
The output will look similar to this:
ID: abcdef123456
Hostname: labex-vm
Status: Ready
Availability: Active
Manager Status:
Address: 172.17.0.2:2377
Reachability: Reachable
Leader: Yes
Platform:
Architecture: x86_64
OS: linux
Resources:
NanoCPUs: 2000000000
MemoryBytes: 4177903616
Engine Version: 20.10.21
This pretty-printed output is very useful for quickly checking the status and key details of a node in your Docker Swarm.
Summary
In this lab, we learned how to use the docker node inspect command to view detailed information about Docker Swarm nodes. We started by inspecting a single node using its ID after initializing a Docker Swarm.
We then explored how to inspect the current node using the alias 'self'. Finally, we learned how to format the output to display specific information and how to pretty-print the node details for better readability.



