Home > database >  Find IP address of docker container running inside docker swarm
Find IP address of docker container running inside docker swarm

Time:10-04

I have a docker swarm cluster. I deployed a Elassandra docker image. Now I want to find that the docker container's IP address for the seed node:

The following are my services in my swarm:

docker service ls

ID NAME
yjehoql7l976 elassandra_seed

I want find the IP address of the container for the Elassandra_seed node by its name to be used in my other docker compose file. Is that possible?

CodePudding user response:

It is possible to find the IP address but remember it will change everytime the service restarts. Let's move onto finding the ip address.

  1. Find the node where your service is running. Run docker service ps stack_service. Check the node column to find the name of the node.
  2. Go to the previously found worker node. Run docker network ls. Usually the stack's network is named like this $stackname_network. Grab the ID of the network.
  3. Describe the network by running docker inspect $networkid. This will show you the service name and its assigned IP address.

CodePudding user response:

Following @rodrigo-loza idea, what I did is:

  1. Use docker service ps for getting the list of services running on my swarm. Add a filter condition to have only those that are actually running (--filter desired-state=running) and format the output to only show service ID and Name
  2. For each service returned by previous command, do a docker inspect and get the address that you need. This, can be done using format (check documentation about Format command and log output for more details regarding the syntax). It is something like --format '{{range $conf := (index (index .NetworksAttachments))}}{{.Addresses}}{{end}}'

Putting all together in bash should look like:

docker service ps --filter desired-state=running \
        --format "{{.ID}} {{.Name}}" \
        elassandra_seed | while read id name ignore 
        do
            echo "$name $(docker inspect \
                      --format '{{range $conf := (index (index .NetworksAttachments))}}{{.Addresses}}{{end}}' \
                      $id)"
        done
elassandra_seed.1 [10.0.0.137/24][172.16.238.181/24]
elassandra_seed.2 [10.0.0.141/24][172.16.238.189/24]
elassandra_seed.3 [10.0.0.147/24][172.16.238.196/24]
elassandra_seed.4 [10.0.0.138/24][172.16.238.183/24]
elassandra_seed.5 [10.0.0.148/24][172.16.238.197/24]
elassandra_seed.6 [10.0.0.139/24][172.16.238.185/24]
elassandra_seed.7 [10.0.0.149/24][172.16.238.198/24]
elassandra_seed.8 [10.0.0.150/24][172.16.238.199/24]
  • Related