13

If you use command:

docker network ls

you'll get the output that lists all Docker networks but lacking the IP range of these networks.

how to get all created sub networks IP ranges?

for example if I use command:

docker network create --subnet 172.31.0.1/16 --internal network-one
docker network create --subnet 173.31.0.1/16 --internal network-two

I would like to get list of the address ranges containing

172.31.0.1/16
173.31.0.1/16

Perfectly if I could get the list as CLI output in a format:

network-one 172.31.0.1/16
network-two 173.31.0.1/16
...

so I could load it as Bash array and parse it later line by line or pipe to another CLI command.

4 Answers 4

19

Try these commands :

docker network inspect $(docker network ls | awk '$3 == "bridge" { print $1}') | jq -r '.[] | .Name + " " + .IPAM.Config[0].Subnet' -
Sign up to request clarification or add additional context in comments.

Comments

6
docker network inspect $(docker network ls -q)|grep -E "IPv(4|6)A"

1 Comment

This shows the IP addresses for the hosts and networks, without labels, so, for me, it's of limited use
4

This produces the exact output requested:

docker network inspect $(docker network ls -q) | jq -r 'map(to_entries) | map ([.[0].value, .[6].value.Config[0].Subnet ]) | .[] | @tsv' | column -t

Explanation

docker network inspect $(docker network ls -q)
  • Gets a list of all networks IDs, and inspects each of them.
jq -r 'map(to_entries) | map ([.[0].value, .[6].value.Config[0].Subnet ]) | .[] | @tsv'
  • Generates raw output from the JQ command. Explained:
  • map(to_entries) generates a list of the network objects with keys and values
  • map([.[0].value, .[6].value.Config[0].Subnet ]) creates for each item from the previous list an array constructed from an item's 0st value and an item's 6th value, which is an object with an array called Config, of which we take the first entry (0) and from it the Subnet value
  • .[] explodes the generated array into a raw list of arrays
  • @tsv formats the newline-delimited array into a tab-separated values representation
column -t
  • Formats tab-separated values into terminal columns

Comments

0

I use the following command in Bash

docker network ls --format "{{.Name}}" | while read i; do echo $i --- $(docker network inspect $i | grep Subnet); done

It reads network list and then gets Subnet field for each network.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.