Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 548
Paul Soubrane Avatar asked Mar 23 '18 09:03

Paul Soubrane


People also ask

How do I find the IP address of a Docker container?

You can easily get the IP address of any container if you have the name or ID of the container. You can get the container names using the "Docker ps -a" command. This will list all the existing containers.

How do I extract an IP address from a container?

If you run the inspect command on a container, you'll get a bunch of information, in JSON format. Scroll down until you find the key NetworkSettings , there look for the sub-key IPAddress . That's your container's IP address. > docker container inspect ubuntu-ip . . . "NetworkSettings": { . . . "IPAddress": "172.17.

Do Docker containers have their own IPS?

By default, the container is assigned an IP address for every Docker network it connects to. The IP address is assigned from the pool assigned to the network, so the Docker daemon effectively acts as a DHCP server for each container. Each network also has a default subnet mask and gateway.


2 Answers

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
like image 61
hichamx Avatar answered Sep 20 '22 09:09

hichamx


  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": ""
        },
like image 31
vivekyad4v Avatar answered Sep 20 '22 09:09

vivekyad4v