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
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.
A new folder on host machine
mkdir -p ~/new_hosts/
ln /etc/hosts ~/new_hosts/hosts
mount the ~/new_hosts/ into container
docker run -it -v ~/new_hosts/:/new_hosts centos /bin/bash
Config dnsmasq use /new_hosts/hosts to resolve name.
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/hostscan change. But the new hosts can't resolve. Becausednsmasquseinotifylisten change event. When you modify a file on the host machine. Thednsmasqcan'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/hostsfile content to another file every time. And change thednsmasqconfiguration to use the new file.