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

江枫思渺然 提交于 2020-07-04 08:56:08

问题


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.


回答1:


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



回答2:


You can list all networks with:

docker network ls

And inspect one network to see its hosts (containers)

docker network inspect <network-id>

You can see which networks a container is connected to inspecting that container:

docker inspect <container-id>

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




回答3:


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



回答4:


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']



回答5:


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 ',"'




回答6:


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",


来源:https://stackoverflow.com/questions/44724764/how-do-i-list-all-containers-in-a-user-defined-docker-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!