Is there a way to discover other containers in a docker network using DNS?

I would like to be able to get a list of all containers running on the same docker network from the docker container. Since the built-in DNS docker can give me IP addresses, if I have host names, it seems like it should just give me a list of host names (maybe DNS cannot do this, I don't know).

Other approaches that I thought were for getting a list of containers:

  • Tie the docker jack into a container and use docker ps. Not a good security idea.
  • Use --linkwhich, I believe, places entries in /etc/hosts. I could read them from there, but this view defeats the target, as I would have to know the hostnames when I started the container.

I try to avoid using an external service discovery mechanism, but I would appreciate any suggestions on how to get a list of containers.

+4
source share
1 answer

An easy way to achieve this goal is to execute one or more docker commands in the host to get the necessary information in a loop and save it in a known place (for example, in bash)

while true; do echo `docker ps --format {{.ID}}` > /SOME/KNOWN/FILE; sleep 5; done

and then allow the containers to access this file using volumes.

This is much safer than giving access to the docker socket, and you can improve it to provide all the information you need (ex json with name, ip, uptime, etc.).

0
source

Source: https://habr.com/ru/post/1668154/


All Articles