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.