0

I have created some Docker containers on two different hosts machine, MAc OSX and Ubuntu 16.04

Taken one by one they are served correctly via HTTP by each HOST Machine. The problem comes when I need hosted container to resolve each other on Ubuntu. (On Mac osx everything is working fine)

On both Host machines I have edited the /etc/hosts file like:

192.168.0.13 mycontainer01.dev
192.168.0.13 mycontainer02.dev
192.168.0.13 mycontainer03.dev

(on second host machine I have other IP clearly)

If I ping mycontainer01.dev from (outside) each host machine I get back the IP correct as defined in hosts file.

But If I enter the nginx container:

docker-compose exec nginx  bash

on macosx ping mycontainer01.dev will succeed

on Ubuntu will return error: $ ping myconteiner01.dev

ping: bad address

koalaok
  • 5,075
  • 11
  • 47
  • 91

1 Answers1

0

Because you haven't access to the /etc/hosts file on host machine inside docker container.

A.N.
  • 278
  • 2
  • 13
  • If so... why is it working on my MacOSX host machine instead? – koalaok Apr 18 '17 at 13:10
  • Because MacOS X uses DNS caching (and you need to clear it after editing /etc/hosts under MacOS X): https://www.tekrevue.com/tip/edit-hosts-file-mac-os-x/ Under Linux (on most distirbutions) you haven't DNS caching: http://stackoverflow.com/questions/11020027/dns-caching-in-linux – A.N. Apr 18 '17 at 13:20
  • - First: [using DNS caching on linux](https://linux.die.net/man/8/nscd) - Add /etc/hosts inside docker container. - Mount host /etc directory inside docker container (bad way). – A.N. Apr 18 '17 at 13:37
  • 1
    Or map /etc/hosts inside container: [```docker run -v /etc/hosts:/etc/hosts ```](http://stackoverflow.com/questions/41440650/map-hosts-etc-hosts-in-a-docker-container-having-bridge-networking?rq=1) – A.N. Apr 18 '17 at 13:40
  • Possible a good solution too. And you can generate this from a host machine's /etc/hosts . – A.N. Apr 18 '17 at 13:55
  • Don't get exactly your idea: "And you can generate this from a host machine's..." – koalaok Apr 18 '17 at 14:10
  • I.e.: ```awk '/^[^#].*/ { print("--add-host", $1 ":" $2)}' /etc/hosts``` – A.N. Apr 18 '17 at 14:36