93

I want to make it so that the Docker container I spin up use the same /etc/hosts settings as on the host machine I run from. Is there a way to do this?

I know there is an --add-host option with docker run, but that's not exactly what I want because the host machine's /etc/hosts file may be different on different machines, so it's not great for me to hardcode exact IP addresses/hosts with --add-host.

2
  • 2
    I dare to suggest that maybe you rather want to set a DNS of the container to take the host's /etc/hosts into account (indirectly). I had the same situation and my solution was to edit /etc/docker/daemon.json to point to a simple DNS proxy which reads /etc/hosts. Mar 8, 2018 at 15:28
  • A little late but ... here they say that /etc/hosts of the deamon will be used: docs.docker.com/config/containers/container-networking/… unfortunately this is not working in my case ...
    – Clerenz
    Apr 7, 2018 at 20:31

12 Answers 12

88

Use --network=host in the docker run command. This tells Docker to make the container use the host's network stack. You can learn more here.

4
  • 7
    How would this work on Mac or Windows, which don't have host networking? Mar 8, 2018 at 14:52
  • 12
    A host network is in many cases (stacks, swarms, ...) not an option at all.
    – Clerenz
    Apr 7, 2018 at 20:32
  • 10
    while --network=host will obviously help to resolv this question, everyone who goes with this should be aware of other consequence such as a simple exemple, it will map and open all port mentionned in the Dockerfile to the internet.
    – JOduMonT
    Dec 2, 2019 at 3:28
  • 4
    Emphasizing the comment from @OndraŽižka, "The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server." May 29, 2020 at 20:52
31

Add a standard hosts file -

docker run -it ubuntu cat /etc/hosts

Add a mapping for server 'foo' -

docker run -it --add-host foo:10.0.0.3 ubuntu cat /etc/hosts

Add mappings for multiple servers

docker run -it --add-host foo:10.0.0.3 --add-host bar:10.7.3.21 ubuntu cat /etc/hosts

Reference - Docker Now Supports Adding Host Mappings

3
28

extra_hosts (in docker-compose.yml)

https://github.com/compose-spec/compose-spec/blob/master/spec.md#extra_hosts

Add hostname mappings. Use the same values as the docker client --add-host parameter.

extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"
14

Also you can install dnsmasq to the host machine, by the command:

sudo apt-get install dnsmasq

And then you need to add the file /etc/docker/daemon.json with content:

{
    "dns": ["host_ip_address", "8.8.8.8"],
}

After that, you need to restart the Docker service by command sudo service docker restart

This option forces to use the host DNS options for every Docker container. Or you can use it for a single container, and the command-line options are explained by this link. Also docker-compose options are supported (you can read about it by this link).

5
  • Underrated comment. This is a way better solution.
    – wes
    Aug 26, 2020 at 20:21
  • Nice. But the Docker part doesn't work for Rancher and Kubernetes.
    – qräbnö
    Nov 26, 2020 at 16:24
  • Great answer. Just remember to replace "host_ip_address" with the real ip address of your host 😅
    – rtribaldos
    Sep 7, 2021 at 15:38
  • On Ubuntu see askubuntu.com/a/1170073/710474 if port 53 is already in use
    – Arigion
    Jan 14 at 10:37
  • this is a proper solution. using network-mode:hosts is not. Feb 2 at 4:18
9

If you are using docker-compose.yml, the corresponding property is:

services:
  xxx:
    network_mode: "host"

Source

7

Add this to your run command:

-v /etc/hosts:/etc/hosts
3
  • 16
    From the outset it looks like this should work, but the Docker container's /etc/hosts file is tailored to the container, so replacing the file has consequences.
    – StampyCode
    Feb 5, 2016 at 13:06
  • 2
    It works for me. However when the external /etc/hosts is changed, it is not updated automatically inside the container. It needs a restart to update it.
    – dashohoxha
    Aug 21, 2016 at 16:38
  • 1
    Yes it's work but it's not a good idea... really risky
    – Hantlowt
    May 26, 2021 at 10:24
5

If trusted users start your containers, you could use a shell function to easily "copy" the /etc/hosts entries that you need:

add_host_opt() { awk "/\\<${1}\\>/ {print \"--add-host $1:\" \$1}" /etc/hosts; }

You can then do:

docker run $(add_host_opt host.name) ubuntu cat /etc/hosts

That way you do not have to hard-code the IP addresses.

2

The host machine's /etc/hosts file can't mount into a container. But you can mount a folder into the container. And you need a dnsmasq container.

  1. A new folder on host machine

     mkdir -p ~/new_hosts/
    
     ln  /etc/hosts ~/new_hosts/hosts
    
  2. mount the ~/new_hosts/ into container

     docker run -it -v ~/new_hosts/:/new_hosts centos /bin/bash
    
  3. Config dnsmasq use /new_hosts/hosts to resolve name.

  4. Change your container's DNS server. Use the dnsmasq container's IP address.

If you change the /etc/hosts file on the host machine, the dnsmasq container's /new_hosts/hosts will change.

I found a problem:

The file in dnsmasq container /new_hosts/hosts can change. But the new hosts can't resolve. Because dnsmasq use inotify listen change event. When you modify a file on the host machine. The dnsmasq can't receive the signal so it doesn't update the configuration. So you may need to write a daemon process to read the /new_hosts/hosts file content to another file every time. And change the dnsmasq configuration to use the new file.

1
  • 1
    You can in fact mount /etc/hosts from the host, moreover, you can mount it read-only: -v /etc/hosts:/etc/hosts:ro
    – Akom
    Oct 10, 2020 at 15:53
1

I had the same problem and found that it is likely in contrast with the containerization concept! however I solved my problem by adding each (ip host) pair from /etc/hosts to an existing running container in this way:

  1. docker stop your-container-name
  2. systemctl stop docker
  3. vi /var/lib/docker/containers/*your-container-ID*/hostconfig.json

find ExtraHosts in text and add or replace null with "ExtraHosts":["your.domain-name.com":"it.s.ip.addr"]

  1. systemctl start docker
  2. docker start your-container-name

if you can stop your container and re-run it, you'd have better situation, so just do that. But if you do not want to destroy your containers, just like mine, it would be a good solution.

0

If you are running a virtual machine for running Docker containers, if there are hosts (VMs, etc.) you want your containers to be aware of, depending on what VM software you are using, you will have to ensure that there are entries on the host machine (hosting the VM) for whatever machines you want the containers to be able to resolve.

This is because the VM and its containers will have the IP address of the host machine (of the VMs) in their resolv.conf file.

0

Or just simply do this:

docker exec -i <container name/id> sh -c 'cat > /etc/hosts' < /etc/hosts

-2

IMO, passing --network=host option while running Docker is a better option as suggested by d3ming over other options as suggested by other answers:

  1. Any change in the host's /etc/hosts file is immediately available to the container, which is what probably you want if you have such a requirement at the first place.
  2. It's probably not a good idea to use the -v option to mount the host's /etc/hosts filr as any unintended change by the container will spoil the host's configuration.
1
  • 1
    As mentioned by Clerenz in a comment on the accepted answer, this is only possible in the rare case that you still have a choice for the used network type.
    – marcolz
    Oct 16, 2019 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.