Docker Community Forums

Share and learn in the Docker community.

Docker for mac does not add docker hostname to /etc/hosts

(Ivan Babrou) #1

Expected behavior

This should ping localhost.

docker run --rm -it --net host debian:jessie ping -c 1 docker

Actual behavior

$ docker run --rm -it --net host debian:jessie ping -c 1 docker
ping: unknown host


Version 1.11.0-beta6 (build: 5404)

(Ivan Babrou) #2

Still an issue in 1.11.0.

(Chulki Lee) #3

Should docker be resolvable on docker containers? docker on ubuntu failed too, so I don’t think it’s a problem specific to Docker for Mac

(David Maze) #4

No; beta 9 made the port publishing mechanism much closer to what’s on Linux, and the artificial docker.local hostname (which was never published to containers) is gone. You need to use normal container linking, or use the DNS-based container lookup on a private Docker network, or whatever other service discovery system you use.

(Chulki Lee) #5

I know - I just don’t understand why you expect docker resolved to localhost.

(Ivan Babrou) #6

I expect $(hostname -s) to resolve to something, not specifically localhost:


$ docker run --rm -it debian:jessie
root@2bfefe49aaf5:/# ping -c 1 $(hostname -s)
PING 2bfefe49aaf5 ( 56 data bytes
64 bytes from icmp_seq=0 ttl=64 time=0.079 ms
--- 2bfefe49aaf5 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.079/0.079/0.079/0.000 ms

Bad (--net host):

$ docker run --rm -it --net host debian:jessie
root@docker:/# ping -c 1 $(hostname -s)
ping: unknown host

(Chulki Lee) #7

Oh got it. See the table at the bottom for the test results (v1.11.0-beta9 used). As you pointed out, docker for mac failed to handle hostname when net=host.

I think it’s good to have the same behavior on docker for mac. I guess it should return the hostname of the mac.

I’m curious why you need it. I guess some apps use hostname to determine its environment (e.g. newrelic), but I doubt they actually send some traffic to the hostname.

| env                      | hostname            | ping | hostname (net=host)            | ping (net=host) |
| native (docker on linux) | docker container id | ok   | hostname                       | ok              |
| docker-machine           | docker container id | ok   | hostname (docker-machine name) | ok              |
| docker for mac           | docker container id | ok   | "docker"                       | failed          |
docker run --rm -it debian:jessie sh -c 'hostname -s; ping -c 1 $(hostname -s)'
docker run --rm -it --net host debian:jessie sh -c 'hostname -s; ping -c 1 $(hostname -s)'

(Ivan Babrou) #8

I need it because things tend to bind to the hostname they are on. Some other things also tend to report hostname and/or hostname’s IP as own address. Mesos and HBase do these things among others.

Is there a way to connect do a docker container running with host networking on mac? I can’t find how to do it in docs.

(Justin Cormack) #9

Not sure if we are going to support this, it is not normal to set this when you use dhcp, which we do at present. There is currently no way to connect from the host if you use host networking, as you saw, so it is not very useful, so making changes to support its use is not a priority at the moment. The routing may change at some point in the future, but we are waiting on bug fixes from Apple to make a decision on that.