简体   繁体   English

如何获取docker网络上的所有IP地址?

[英]How to get all ip addresses on a docker network?

I have containers running in a swarm stack of services (on different docker-machines each) connected together on an overlay docker network.我有容器在一组服务(在每个不同的 docker 机器上)中运行,这些服务在覆盖 docker 网络上连接在一起。

How would it be possible to get all used ip adresses on the network associated with their services or container name from inside a container on this network ?如何从该网络上的容器内部获取与其服务或容器名称相关联的网络上所有使用过的 IP 地址?

Thank you谢谢

If you want to execute this command from inside containers, first you have to mount docker.sock for each service (assuming that docker is installed in the container) 如果要从容器内执行此命令,首先必须为每个服务安装docker.sock(假设docker安装在容器中)

volumes:
  - /var/run/docker.sock:/var/run/docker.sock

then in each container you have to install jq and after that you can simply run docker network inspect <network_name_here> | jq -r 'map(.Containers[].IPv4Address) []' 然后在每个容器中你必须安装jq ,之后你可以简单地运行docker network inspect <network_name_here> | jq -r 'map(.Containers[].IPv4Address) []' docker network inspect <network_name_here> | jq -r 'map(.Containers[].IPv4Address) []' expected output something like: docker network inspect <network_name_here> | jq -r 'map(.Containers[].IPv4Address) []'预期输出类似于:

172.21.0.2/16
172.21.0.5/16
172.21.0.4/16
172.21.0.3/16

Assuming you're using the default VIP endpoint, you can use DNS to resolve the IP's of a service. 假设您使用的是默认VIP端点,则可以使用DNS来解析服务的IP。 Here's an example of using dig to get VIP IP using and then get the individual container IP's behind that VIP using tasks. 以下是使用dig获取VIP IP的示例,然后使用任务获取VIP后面的单个容器IP。

docker network create --driver overlay --attachable sweet

docker service create --name nginx --replicas=5 --network sweet nginx

docker container run --network sweet -it bretfisher/netshoot dig nginx
~~~
;; ANSWER SECTION:
nginx.          600 IN  A   10.0.0.3
~~~

docker container run --network sweet -it bretfisher/netshoot dig tasks.nginx
~~~
;; ANSWER SECTION:
tasks.nginx.        600 IN  A   10.0.0.5
tasks.nginx.        600 IN  A   10.0.0.8
tasks.nginx.        600 IN  A   10.0.0.7
tasks.nginx.        600 IN  A   10.0.0.6
tasks.nginx.        600 IN  A   10.0.0.4
~~~
  1. Find the name OR ID of overlay network - 找到覆盖网络的名称或ID -

    $ docker network ls | grep overlay

  2. Do a inspect - 做检查 -

    docker inspect $NETWORK_NAME

You will be able to find the container names & IPs allocated to them. 您将能够找到分配给它们的容器名称和IP。 You can do a fetch/grep the required values from the inspect output. 您可以从inspect输出中获取/ grep所需的值。 You will find the output something as below - 你会发现输出如下 -

    "IPAM": {
        "Driver": "default",
        "Options": null,
        "Config": [
            {
                "Subnet": "172.23.0.0/16",
                "Gateway": "172.23.0.1"
            }
        ]
    },
    "Internal": false,
    "Attachable": true,
    "Ingress": false,
    "ConfigFrom": {
        "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
        "183584efd63af145490a9afb61eac5db994391ae94467b32086f1ece84ec0114": {
            "Name": "emailparser_lr_1",
            "EndpointID": "0a9d0958caf0fa454eb7dbe1568105bfaf1813471d466e10030db3f025121dd7",
            "MacAddress": "02:42:ac:17:00:04",
            "IPv4Address": "172.23.0.4/16",
            "IPv6Address": ""
        },
        "576cb03e753a987eb3f51a36d4113ffb60432937a2313873b8608c51006ae832": {
            "Name": "emailparser",
            "EndpointID": "833b5c940d547437c4c3e81493b8742b76a3b8644be86af92e5cdf90a7bb23bd",
            "MacAddress": "02:42:ac:17:00:02",
            "IPv4Address": "172.23.0.2/16",
            "IPv6Address": ""
        },

First, find the name of the network which your swarm is using. 首先,找到您的群使用的网络名称。

Then run docker network inspect <NETWORK-NAME> . 然后运行docker network inspect <NETWORK-NAME> This will give you a JSON output, in which you'll find an object with key "Containers". 这将为您提供JSON输出,您将在其中找到具有键“Containers”的对象。 This object reveals all the containers in the network and their IP addresses respectively. 此对象分别显示网络中的所有容器及其IP地址。

for n in `docker network ls | awk '{print $1}'`; do docker network inspect $n; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM