I would like to be able to get a list of all containers running on the same docker network from within a docker container. As the built in docker DNS can give me the IP addresses if I have the hostnames, it seems like it should be able to just give me a list of hostnames (maybe DNS cannot do this, I don't know).
Other approaches that I've thought of for getting a list of containers:
docker ps
. Not a great idea as far as security goes.--link
which I believe places entries in /etc/hosts
. I could then read them from there, but this sort of defeats the purpose as I would have to already know the host names when I launched the container.I'm looking to avoid using an external service discovery mechanism, but I would appreciate all suggestions for how to get a list of containers.
An easy way to achieve this would be running a one or more docker command(s) in the host, to get the information you need in a loop and store it in a known location (ex in bash)
while true; do echo `docker ps --format {{.ID}}` > /SOME/KNOWN/FILE; sleep 5; done
and then let the containers access this file, using volumes.
It is much safer than providing access to the docker socket, and you can improve it to provide all the information you need (ex json with name, ip, running time, etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With