How to use docker network ls command to list and filter networks

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker network ls command to list and filter Docker networks. You will start by listing all available networks, then explore how to display full network IDs using the --no-trunc option.

Furthermore, you will practice filtering networks based on various criteria such as driver, name, scope, and type. Finally, you will learn how to format the output of the docker network ls command using a template for customized display. This lab will equip you with the essential skills to manage and inspect Docker networks using the command line.


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/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/ls -.-> lab-555177{{"How to use docker network ls command to list and filter networks"}} docker/network -.-> lab-555177{{"How to use docker network ls command to list and filter networks"}} end

List all Docker networks

In this step, you will learn how to list all the Docker networks available on your system. Docker networks are essential for containers to communicate with each other and with the outside world. By default, Docker creates three networks: bridge, host, and none.

To list all Docker networks, you use the docker network ls command. This command provides a list of networks, including their ID, name, driver, and scope.

Let's execute the command to see the available networks.

docker network ls

You should see output similar to this:

NETWORK ID     NAME      DRIVER    SCOPE
b432a7b0f123   bridge    bridge    local
f567c8d1e234   host      host      local
a987b6c5d4e3   none      null      local

The output shows the default networks. The NETWORK ID is a unique identifier for the network. The NAME is the name of the network. The DRIVER is the network driver used by the network (e.g., bridge, host, null). The SCOPE indicates where the network is available (e.g., local).

List networks without truncating the ID

In the previous step, you listed Docker networks and noticed that the NETWORK ID is truncated. This is the default behavior to keep the output concise. However, sometimes you need the full network ID, for example, when referencing a specific network in other Docker commands.

To display the full network ID, you can use the --no-trunc option with the docker network ls command.

Let's try this command to see the full network IDs.

docker network ls --no-trunc

The output will now show the complete network IDs:

NETWORK ID                                                       NAME      DRIVER    SCOPE
b432a7b0f1234567890abcdef1234567890abcdef1234567890abcdef12345678 bridge    bridge    local
f567c8d1e234567890abcdef1234567890abcdef1234567890abcdef12345678 host      host      local
a987b6c5d4e3210fedcba9876543210fedcba9876543210fedcba9876543210fed none      null      local

As you can see, the NETWORK ID column now displays the full, untruncated IDs. This is useful when you need to copy and paste the complete ID for further operations.

Filter networks by driver

In this step, you will learn how to filter the list of Docker networks based on their driver. This is useful when you want to see only networks using a specific driver, such as bridge or host.

You can use the --filter option with the docker network ls command to filter the output. The filter syntax is key=value. To filter by driver, you use the key driver.

Let's filter the networks to show only those using the bridge driver.

docker network ls --filter driver=bridge

The output will now only show networks with the bridge driver:

NETWORK ID     NAME      DRIVER    SCOPE
b432a7b0f123   bridge    bridge    local

Now, let's filter the networks to show only those using the host driver.

docker network ls --filter driver=host

The output will now only show networks with the host driver:

NETWORK ID     NAME      DRIVER    SCOPE
f567c8d1e234   host      host      local

You can use this filtering capability to quickly find networks based on their underlying technology.

Filter networks by name

In this step, you will learn how to filter the list of Docker networks based on their name. This is useful when you are looking for a specific network by its name.

Similar to filtering by driver, you use the --filter option with the docker network ls command. To filter by name, you use the key name.

Let's filter the networks to show only the network named bridge.

docker network ls --filter name=bridge

The output will now only show the network named bridge:

NETWORK ID     NAME      DRIVER    SCOPE
b432a7b0f123   bridge    bridge    local

Now, let's filter the networks to show only the network named host.

docker network ls --filter name=host

The output will now only show the network named host:

NETWORK ID     NAME      DRIVER    SCOPE
f567c8d1e234   host      host      local

You can use this filter to quickly locate a network when you know its name.

Filter networks by scope and type

In this step, you will learn how to filter the list of Docker networks based on their scope and type. The scope indicates where the network is available (e.g., local or swarm), and the type refers to the network driver. While type is often the same as driver, using type as a filter key is also possible.

You can use the --filter option with the docker network ls command to filter the output. To filter by scope, you use the key scope. To filter by type, you use the key type.

Let's filter the networks to show only those with a local scope.

docker network ls --filter scope=local

The output will show all networks with the local scope:

NETWORK ID     NAME      DRIVER    SCOPE
b432a7b0f123   bridge    bridge    local
f567c8d1e234   host      host      local
a987b6c5d4e3   none      null      local

Now, let's filter the networks to show only those with a bridge type.

docker network ls --filter type=bridge

The output will show networks with the bridge type (which is the same as the bridge driver in this case):

NETWORK ID     NAME      DRIVER    SCOPE
b432a7b0f123   bridge    bridge    local

You can combine filters by using the --filter option multiple times. For example, to filter for networks with local scope and bridge driver:

docker network ls --filter scope=local --filter driver=bridge

This will give you the same result as filtering by type=bridge in this specific environment, but it demonstrates how to combine filters.

Format network output using a template

In this step, you will learn how to format the output of the docker network ls command using a Go template. This is a powerful feature that allows you to customize the output to display only the information you need and in a specific format.

The --format option allows you to specify a template. The template uses Go's text/template package syntax. You can access various fields of the network object, such as .ID, .Name, .Driver, and .Scope.

Let's format the output to show only the network name and driver, separated by a colon.

docker network ls --format "{{.Name}}: {{.Driver}}"

The output will now be formatted according to the template:

bridge: bridge
host: host
none: null

You can also include headers in your formatted output.

docker network ls --format "Network Name\tDriver\n{{.Name}}\t{{.Driver}}"

The output will now include headers:

Network Name    Driver
bridge          bridge
host            host
none            null

The \t creates a tab space, and \n creates a new line. This formatting capability is very useful for scripting and generating reports.

Summary

In this lab, you learned how to use the docker network ls command to list Docker networks. You started by listing all available networks, observing the default networks and their basic information (ID, name, driver, scope). You then explored how to display the full network ID using the --no-trunc option, which is useful for referencing specific networks.

Furthermore, you practiced filtering network lists based on various criteria. You learned to filter by driver using the --filter driver=<driver_name> option, by network name using --filter name=<network_name>, and by scope and type using --filter scope=<scope> and --filter type=<type>. Finally, you discovered how to format the output of the docker network ls command using the --format option and Go templates, allowing you to customize the displayed information.