0

I have two containers I've started as:

-name foo --hostname mynet.foo

-name bar --hostname bar

From foo container I can ping bar using ping bar but from bar container I only can ping foo container using ping foo (the container name). If I try to ping foo using ping mynet.foo I have a bad address: mynet.foo.

I need to refer the hosts as the --hostname value not the -name value.

From this: the --hostname is the local hostname of the container (i.e. the one it sees itself). Insane!

Magno C
  • 1,922
  • 4
  • 28
  • 53

1 Answers1

1

To access container's via their hostname you can simply add a host entry for that.

For example:

docker run -tid --name foo --add-host=bar:<ip-address-of-bar-container> -h  mynet.foo <image-name>

docker run -tid --name bar --add-host=mynet.foo:<ip-address-of-foo-container> -h bar <image-name>

With this you will be able to ping mynet.foo from bar container and vice-versa.

  • Wait.... how can I do this without know the IP address? Need I to create a complete DNS in the `run` command line? I can't even know the network IP range because I want to keep the things simple. – Magno C Jun 28 '18 at 10:18
  • 1
    You can refer to this link for dns settings https://stackoverflow.com/questions/37242217/access-docker-container-from-host-using-containers-name/45071126#45071126 – Shivani Kothari Jun 28 '18 at 15:48
  • Fantastic! Thanks. – Magno C Jun 29 '18 at 12:42