11

I have containers running in a swarm stack of services (on different docker-machines each) connected together on an overlay docker network.

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?

Thank you

shay__
  • 3,815
  • 17
  • 34
Paul Soubrane
  • 111
  • 1
  • 1
  • 5

5 Answers5

10
  1. Find the name OR ID of overlay network -

    $ 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. You can do a fetch/grep the required values from the inspect output. 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": ""
        },
vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
  • Thank you for your answer. I am looking for a way to do it from inside a container on the network... I update my post to make it clearer. – Paul Soubrane Mar 23 '18 at 09:43
10

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)

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) []' expected output something like:

172.21.0.2/16
172.21.0.5/16
172.21.0.4/16
172.21.0.3/16
hichamx
  • 794
  • 3
  • 7
  • 18
4

Assuming you're using the default VIP endpoint, you can use DNS to resolve the IP's of a service. 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.

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
~~~
Bret Fisher
  • 8,164
  • 2
  • 31
  • 36
4
for n in `docker network ls | awk '!/NETWORK/ {print $1}'`; do docker network inspect $n; done
Hans Poo
  • 804
  • 1
  • 8
  • 17
  • 1
    "Error: No such network: NETWORK". Without the column name: for n in `docker network ls | awk '{i = FRN + 2} i != 0 && FNR >= i {print $1}'`; do docker network inspect $n; done – Davlio Sep 06 '22 at 19:36
  • 1
    Thanks Davlio, i've edited the answer but with a more explicit aproach to skip the column header: for n in `docker network ls | awk '!/NETWORK/ {print $1}'`; do docker network inspect $n; done – Hans Poo Sep 08 '22 at 11:08
2

First, find the name of the network which your swarm is using.

Then run docker network inspect <NETWORK-NAME>. This will give you a JSON output, in which you'll find an object with key "Containers". This object reveals all the containers in the network and their IP addresses respectively.

Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • Thank you for your answer. I am looking for a way to do it from inside a container on the network... I updated my post to make it clearer. – Paul Soubrane Mar 23 '18 at 09:46