How to use docker network inspect command to view network details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker network inspect command to view detailed information about Docker networks. You will begin by creating a custom Docker network to work with.

Following the network creation, you will explore different ways to inspect its configuration. This includes using the default JSON output, applying a Go template for customized output, and utilizing the verbose option for more extensive details. Finally, you will learn how to inspect multiple networks simultaneously.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/ls -.-> lab-555176{{"How to use docker network inspect command to view network details"}} docker/inspect -.-> lab-555176{{"How to use docker network inspect command to view network details"}} docker/create -.-> lab-555176{{"How to use docker network inspect command to view network details"}} docker/network -.-> lab-555176{{"How to use docker network inspect command to view network details"}} end

Create a custom Docker network

In this step, you will learn how to create a custom network in Docker. By default, Docker provides several network drivers, but creating a custom network allows you to isolate your containers and control how they communicate with each other.

Before creating a network, let's check the existing networks on your system using the docker network ls command.

docker network ls

You should see some default networks like bridge, host, and none.

Now, let's create a new custom network named my-custom-network using the bridge driver. The bridge driver is the default network driver and is suitable for most use cases.

docker network create my-custom-network

After executing the command, Docker will create the network and print its ID.

To verify that the network has been created successfully, run the docker network ls command again.

docker network ls

You should now see my-custom-network listed among the available networks.

Inspect the custom network using default JSON format

In this step, you will learn how to inspect the details of a Docker network using the docker network inspect command. By default, this command outputs the network configuration in JSON format, which provides comprehensive information about the network, including its ID, name, driver, subnet, gateway, and connected containers.

To inspect the my-custom-network you created in the previous step, use the following command:

docker network inspect my-custom-network

This command will output a detailed JSON object representing the configuration of my-custom-network. You can examine this output to understand the network's properties.

The output will include fields such as:

  • Name: The name of the network (my-custom-network).
  • Id: The unique ID of the network.
  • Driver: The network driver used (bridge).
  • Scope: The scope of the network (usually local).
  • IPAM: Information about IP Address Management, including the subnet and gateway.
  • Containers: A list of containers connected to this network (initially empty).

Understanding the output of docker network inspect is crucial for debugging network issues and understanding how your containers are connected.

Inspect the custom network using a Go template

In this step, you will learn how to use Go templates with the docker network inspect command to extract specific information from the network configuration. This is useful when you only need certain details and don't want to parse the entire JSON output.

The -f or --format flag allows you to specify a Go template. The template uses the Go template syntax to access fields within the JSON output.

Let's say you only want to see the network's ID and driver. You can use the following template: {{.Id}} {{.Driver}}.

Execute the following command to inspect my-custom-network and format the output using this template:

docker network inspect -f '{{.Id}} {{.Driver}}' my-custom-network

The output will be the network ID followed by the driver name, separated by a space.

You can access nested fields as well. For example, to get the subnet from the IPAM configuration, you can use the template {{.IPAM.Config}}.. Since IPAM.Config is a list, you might need to iterate through it or access a specific element if there are multiple configurations. For simplicity, let's assume there's only one configuration and access the Subnet field within the first element of the Config list: {{(index .IPAM.Config 0).Subnet}}.

Let's try inspecting the network and displaying the network name and the subnet:

docker network inspect -f 'Name: {{.Name}}, Subnet: {{(index .IPAM.Config 0).Subnet}}' my-custom-network

This command will output the network name and its subnet in a more readable format.

Using Go templates provides a powerful way to customize the output of Docker commands and extract exactly the information you need.

Inspect the custom network with verbose output

In this step, you will learn how to get more detailed information when inspecting a Docker network by using the --verbose or -v flag. This flag provides additional details that are not included in the default output, which can be helpful for advanced debugging and understanding the network's internal state.

To inspect my-custom-network with verbose output, use the following command:

docker network inspect -v my-custom-network

The output will be similar to the default JSON output, but it will include extra fields and details. The exact additional information can vary depending on the network driver and Docker version, but it often includes more low-level details about the network's configuration and state.

For example, with the bridge driver, verbose output might include details about the underlying bridge interface on the host system, including its MAC address and IP address. It might also show more detailed information about the IPAM configuration.

While the default output is usually sufficient for most purposes, the verbose output can be invaluable when you need to delve deeper into the network's configuration or troubleshoot complex networking issues.

Compare the output of this command with the output from the previous step to see the additional information provided by the verbose flag.

Inspect multiple networks

In this step, you will learn how to inspect multiple Docker networks simultaneously using the docker network inspect command. This can be useful when you need to compare the configurations of different networks or get information about several networks at once.

To inspect multiple networks, simply provide the names or IDs of the networks as arguments to the docker network inspect command, separated by spaces.

Let's inspect the my-custom-network you created and the default bridge network.

docker network inspect my-custom-network bridge

This command will output the JSON configuration for both my-custom-network and the bridge network. The output for each network will be a separate JSON object.

You can also combine inspecting multiple networks with the formatting options you learned in the previous steps. For example, to get the name and driver of both networks using a Go template:

docker network inspect -f 'Name: {{.Name}}, Driver: {{.Driver}}' my-custom-network bridge

This will output the name and driver for each specified network on a separate line.

Inspecting multiple networks at once can significantly streamline your workflow when managing several networks in your Docker environment.

Summary

In this lab, you learned how to create a custom Docker network using the docker network create command and verify its creation with docker network ls. You then explored the docker network inspect command to view detailed network configurations.

You practiced inspecting a network using the default JSON output, utilizing a Go template for customized output, and obtaining verbose information. Finally, you learned how to inspect multiple networks simultaneously.