'Find IP address of docker container running inside docker swarm
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?
Solution 1:[1]
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.
- 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. - 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. - Describe the network by running
docker inspect $networkid
. This will show you the service name and its assigned IP address.
Solution 2:[2]
Following @rodrigo-loza idea, what I did is:
- 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 serviceID
andName
- 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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rodrigo Loza |
Solution 2 | OnaBai |