Sub topic 4
Listing and inspecting networks
This chapter assumes you can run containers with docker run, read JSON output, and understand IP addresses, subnets, and gateways. It also assumes you know that the default bridge network exists, as covered in the previous chapter. Here you learn to see what networks exist and which containers are attached to them. You will use these commands in every later chapter.
Listing networks
docker network ls prints every network known to the local Docker daemon.
docker network lsA fresh host typically shows three networks:
NETWORK ID NAME DRIVER SCOPE
3f1c2a9b7d10 bridge bridge local
a8e4c0d5b912 host host local
5d7b6e1f0c33 none null local- NAME is what you pass to
--network. - DRIVER is the network type, such as
bridge,host,null,overlay, ormacvlan. - SCOPE is
localwhen the network exists on this host only. Overlay networks in swarm mode showswarm.
You can filter and trim the output:
docker network ls --filter driver=bridge
docker network ls --format '{{.Name}}\t{{.Driver}}'Inspecting one network
docker network inspect prints a JSON document describing a network. Create a network and a container to inspect:
docker network create app-net
docker run -d --name web --network app-net nginx:alpine
docker network inspect app-netThe fields you will use most often are:
| Field | Meaning |
|---|---|
| `Driver` | The driver that implements the network |
| `IPAM.Config` | The subnet and gateway Docker assigned |
| `Containers` | Containers attached now, with their IPs and MAC addresses |
| `Internal` | `true` if the network has no external access |
| `Options` | Driver options, such as the bridge name |
IPAM stands for IP address management. It is the part of Docker that hands out addresses from the subnet.
Extracting just what you need
The full JSON is long. The --format option takes a Go template, so you can print one part of it.
docker network ls
docker network inspect app-net --format '{{json .Containers}}'The second command prints the attached containers as JSON, including each IP address. To print the subnet only:
docker network inspect app-net --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}'Inspecting from the container side
A container can be on several networks. To see all of them from the container's point of view, inspect the container and read NetworkSettings.Networks:
docker inspect web --format '{{json .NetworkSettings.Networks}}'Each entry shows the network's gateway, the container's IP address, and any network aliases. Use the network view to ask "who is on this network?" and the container view to ask "where is this container attached?"
Common errors
Error response from daemon: network xyz not found: the name is misspelled, or the network exists on another host or Docker context.- Empty
Containersobject: stopped containers are not listed. Only running containers appear in the network view. - Unexpected subnet: if you did not pass
--subnet, Docker picks one from its default pool. It can overlap with a VPN or corporate range, which causes routing problems.
Exercise
You run docker network inspect app-net and the Containers field is empty, but you know a container named web was created on that network. What is the most likely reason, and how do you confirm it?
Show answer
The container is probably stopped, because only running containers are listed. Confirm with docker ps -a --filter name=web to see its status, and start it with docker start web. You can also run docker inspect web --format '{{json .NetworkSettings.Networks}}' to check whether it was attached to app-net at all.
Exercise
Write a command that prints only the gateway of the network app-net.
Show answer
docker network inspect app-net --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}'