22

How do I get a list of all the containers in a user-defined docker network?

I would like to get all the commit hashes of every container for a specific user-defined docker network.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Viktor Vojnovski
  • 1,191
  • 1
  • 7
  • 19

9 Answers9

24
docker network inspect \
  -f '{{ range $key, $value := .Containers }}{{ printf "%s\n" $key}}{{ end }}' \
  <network-id>

will give a newline delimited list of the container hashes belonging to the network-id network.

You can adjust the printf format to get other syntax (comma-separated, tab-separated, container id+name, etc). For example:

 $ docker network inspect \
    -f '{{ range $key, $value := .Containers }}{{printf "%s: %s\n" $key .Name}}{{ end }}' \
    web-ingress
0b7f82ad7535ac7c1a454aaf0a5df6b87547d3cb9d751d0e0e1b4952a849b11b: mon_grafana.1.v5i0ea6nv12k0h3eo8egglg6n
1cce723642af2ce9382c7d46cca868d4674b4645d07458eeed9e928d29a4fb1f: mon_prometheus.1.lsfdcig6uhqfbbv7n07irpj3j
1f4840710f77fa1e02bd3e95581139b9f3c13fe4c857ce6ac44bfbdae4916920: mon_alertmanager.1.kwvw7kqsfpi9qzpmqdrd85yc7
2efea443fee41fd5dbca714145ca6ff95d91be9c60a469be597aadfaca90914d: mon_unsee.1.3lg8qgnvshibklnypzt5rw95s
6fdb893488f6e56766501e763d4c60196ae12a22ee9bd204d84fe324331714e8: mon_dashboard.1.r6b6nhatwmk88y5ncgagnaxh4
lb-web-ingress: web-ingress-endpoint
Hamy
  • 20,662
  • 15
  • 74
  • 102
Viktor Vojnovski
  • 1,191
  • 1
  • 7
  • 19
18

You can list all networks with:

docker network ls

And inspect one network to see its hosts (containers)

docker network inspect <network-id>

Inspecting that a container, you can see which network it is connected to:

docker inspect <container-id>

More info in the docs: https://docs.docker.com/engine/userguide/networking/work-with-networks/#create-networks

Robert
  • 33,429
  • 8
  • 90
  • 94
  • I found that when a container has been brought up with an attribute of network_mode: "service: " that the 'docker inspect' or 'docker network inspect ' commands are not going to give you the information as detailed above. What you will see is something like this: "NetworkMode": "container: " – Mad Max Jan 27 '22 at 00:38
1

Another way using pure bash and no python - easier to remember for me

docker network inspect YOUR_NETWORK_ID | grep Name | tail -n +2 | cut -d':' -f2 | tr -d ',"'

Sample output

container1
container2
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
1

Solution can be simple as below as well. To list name of the container only

docker network inspect NETWORK ID | grep "Name" | awk -F":" '{print $2}' | tr -d ',"'

enter image description here

7ochem
  • 2,183
  • 1
  • 34
  • 42
kartik
  • 311
  • 5
  • 6
1

A simple command to list the names of the container using a custom network is:

docker inspect network-name | grep Name

This command will list the name of the network and containers connected to it like below:

"Name": "network-name",
        "Name": "Container1",
        "Name": "Container2",
Surya
  • 971
  • 2
  • 17
  • 30
0

This is my bash command using python's json parsing

docker network inspect YOUR_NETWORK_ID \
    | python -c "import sys, json; print([v['Name'] for k,v in json.load(sys.stdin)[0]['Containers'].items()])"

Sample output

[u'container1', u'container2']
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
0

I found that when a container has been brought up with an attribute of

network_mode: "service: <some service name>"

that the 'docker inspect' or 'docker network inspect ' commands are not going to give you the information as detailed above.

What you will see instead is something like this:

"NetworkMode": "container: <some container id representing the service>"

Mad Max
  • 535
  • 4
  • 8
0

Allow me to contribute with another way of obtaining the result, still using docker network inspect, but providing other possibilities in terms of data inspection and output format.


You list the available networks for Docker:

$ docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
a1682358dfa3   bridge        bridge    local
c6910d1da750   host          host      local
c9790e542025   none          null      local
9b265ecb4042   web_default   bridge    local

You inspect the network, with output in JSON, accessing the Containers object + prettifying the JSON:

$ docker network inspect web_default -f '{{json .Containers}}' | python3 -m json.tool
{
    "5d05c2606b022b0c9bc480f138b8f8f0e382dca4b1decb1c27181a66e641803f": {
        "Name": "django",
        "EndpointID": "b6bf6b1dc5437f2359fb550368a5bc6529cd80fa5b9ece3c752eecef6627ea57",
        "MacAddress": "02:42:ac:12:00:03",
        "IPv4Address": "172.18.0.3/16",
        "IPv6Address": ""
    },
    "b971715bd0b3560831b03e86bcf2151ed04a60a46ad90b24f1f2265617360344": {
        "Name": "postgres",
        "EndpointID": "5883f465e235c66327271c1dfd05372f02012efa78928dd447c5444b0468647c",
        "MacAddress": "02:42:ac:12:00:02",
        "IPv4Address": "172.18.0.2/16",
        "IPv6Address": ""
    }
}

You can even redirect the command output to a for further inspection:

$ docker network inspect web_default -f '{{json .Containers}}' | python3 -m json.tool > web_default_containers.json
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
0

Here is the most elegant solution I could come up with:

docker inspect test -f '{{range.Containers}}{{println .Name}}{{end}}' | awk NF

The extra awk call is to remove the annoying empty line at the end.

Boeboe
  • 2,070
  • 1
  • 17
  • 21