How to get all ip addresses on a docker network?

Paul Soubrane picture Paul Soubrane · Mar 23, 2018 · Viewed 8.9k times · Source

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

Answer

hichamx picture hichamx · Mar 28, 2018

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