3

I have a network in docker filled with containers. Some are unnamed and can be found by ip. Others are named and can be accessed with a dns lookup.

How do I view all the resolvable domains in a docker network? If it's not possible to view all, how to view all the domains which reach a certain container?

arshbot
  • 12,535
  • 14
  • 48
  • 71
  • Does this answer your question? [How do I list all containers in a user-defined docker network?](https://stackoverflow.com/questions/44724764/how-do-i-list-all-containers-in-a-user-defined-docker-network) – Yarden Shoham Jun 26 '20 at 11:26
  • No, domains and containers are not the same. In certain situations, they are related - but I'm not looking for indications of my containers on a network. I'm explicitly looking for domains ( not ip ) registered with the dns server – arshbot Jun 26 '20 at 15:28

1 Answers1

1

In a default bridge network containers can access each other by IP. In a user-defined bridge network they can also use names and aliases. Additionally you can specify a network explicitly (name-or-alias -> name-or-alias.network).

To list aliases in a network:

for c in `docker ps -q`; do \
    docker inspect "$c" \
        -f $'{{range $k, $v := .NetworkSettings.Networks}}{{$k}} {{.Aliases}}\n{{end}}'; \
done | awk '$1 == "<network>"'

Output:

<network> [<alias1> <alias2>...]
...

E.g.:

prj_default [app 59bb8a265b9b]
...

To list container names and aliases across all networks:

for c in `docker ps -q`; do \
    echo -n "`docker inspect "$c" -f '{{.Name}}'` ($c): "; \
    docker inspect "$c" \
        -f '{{range $k, $v := .NetworkSettings.Networks}}{{$k}} {{.Aliases}} {{end}}'; \
done

Output:

/<container_name> (<container_id>): <network1> [<alias1> <alias2>...] <network2> [<alias1> <alias2>...]...
...

E.g.:

/prj_app_1 (59bb8a265b9b): prj_default [app 59bb8a265b9b]
...

To check what a domain resolves to in a container:

$ getent hosts app
192.168.224.5     app  app

To list name and aliases of a specific container:

docker inspect 59bb8a265b9b \
    -f '{{.Name}} {{range $k, $v := .NetworkSettings.Networks}}{{$k}} {{.Aliases}} {{end}}'

More on it here.

x-yuri
  • 16,722
  • 15
  • 114
  • 161