Rob Yurkowski

  • About Me
  • Services
  • Work With Me
  • Blog
  • List Docker Networks by Subnet

    Get a list of docker networks by their subnet (needed when diagnosing / fixing a similar problem to this).

    for id in $(docker network ls --format "{{ .ID }}"); do
        docker network inspect $id --format '{{ json . }}' \
        | jq -c '[.Name,.Driver,.IPAM.Config.[].Subnet]'
    done
    

    This should give you a list of the network name, driver, and subnet, like so:

    ["bridge","bridge","172.17.0.0/16"]
    ["hanami-cli_default","bridge","172.24.0.0/16"]
    ...
    

    You might also encounter several jq: error lines. These occur when the network you’re iterating over doesn’t have any IPAM entries, usually because they’re either the host network or a null network. When I run the command above, I get two such lines.

    If you get more, simply remove ,.IPAM.Config.[].Subnet from the jq call and you can see which networks are returning an error, like so:

    ["bridge","bridge"]
    ["hanami-cli_default","bridge"]
    ["host","host"]
    ["none","null"]