How to resolve docker host names (/etc/hosts) in containers

how is it possible to resolve names defined in Docker host’s /etc/hosts in containers?
Containers running in my Docker host can resolve public names (e.g. so Docker dns is working fine.
I would like to resolve names from Docker hosts’s (e.g. smtp) from containers.

My final goal is to connect to services running in Docker host (e.g. smtp server) from containers. I know I can use the Docker Host IP ( from containers, but I thought that Docker would have used the Docker host /etc/hosts to build containers’s resolve files as well.

  • docker pull through cache for private registry not working
  • Cannot use mongodb (docker) with Symfony 3
  • Running a single Docker Cloud's HAProxy container with SSL and non-SSL simultaneously
  • How to resolve docker machine ip address to a DNS?
  • Docker RUN apt-get -y update && apt-get install -y fortunes
  • Exporting data from MySQL docker image
  • I am even quite sure I have seen this working a while ago… but I could be wrong.

    Any thoughts?


  • Is it correct to run a private Docker registry like a container?
  • Creating a Docker container with runable android tool
  • Maven WAR Build for Tomcat: “Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”
  • Missing js/css files in frontend of contao cms
  • Why does my postgres data disappear after loading it from a backup?
  • What are proven options for using ZFS remotely as a volume backend for docker?
  • 2 Solutions collect form web for “How to resolve docker host names (/etc/hosts) in containers”

    Check out the --add-host flag for the docker command:

    $ docker run --add-host="smtp:" container command

    In Docker, /etc/hosts cannot be overwritten or modified at runtime (security feature). You need to use Docker’s API, in this case --add-host to modify the file.

    For docker-compose, use the extra_hosts option.

    For the whole “connect to services running in host” problem, see the discussion in this GitHub issue:

    The common approach for this problem is to use --add-host with Docker’s gateway address for the host, e.g. --add-host="dockerhost:". Check the issue above for some scripts that find the correct IP and start your containers.

    You can setup on host simple DNS server, and in container setup /etc/resolve.conf to Docker host DNS server.

    For example in dnsmasq you can add addn-hosts=/etc/hosts to config file. So container, by using Docker host DNS server, will be able to resolve hosts /etc/hosts.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.