How can you make the Docker container use the host machine's '/etc/hosts' file?

后端 未结 11 1766
夕颜
夕颜 2020-12-23 11:21

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

相关标签:
11条回答
  • 2020-12-23 11:53

    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).

    0 讨论(0)
  • 2020-12-23 11:53

    All three options work for me.

    1. Using the host's network (--network=host)
    2. Using extra_hosts in the Docker options
    3. Mapping file /etc/hosts from the host to the container
    0 讨论(0)
  • 2020-12-23 11:56

    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.
    0 讨论(0)
  • 2020-12-23 12:03

    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.

    0 讨论(0)
  • 2020-12-23 12:04

    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 讨论(0)
提交回复
热议问题