Sync the /etc/hosts file to the docker image

Hello folks, @martyone !

I used to assign static IP to my devices and assign an alias to the hosts file for them.
With the VirtualBox based SDK it worked without issues, however with the docker based one it does not.

I am not familiar with the docker, but it might be useful to sync the host’s host file to the docker image if possible.

Docker maps the host’s /etc/hosts into the container at startup. Hence you can add the static mapping in your host’s /etc/hosts, you can add some lines to /etc/hosts from command line for docker run with --add-host="...", or you can derive a new image with an ENTRYPOINT, that changes the file.

Docker maps the host’s /etc/hosts into the container at startup.

Hmm somehow it does not work for me:

But since it is a docket issue I will sort it out.

One solution could be creating a specific version of the hosts file, with the entries from the current file + the entries you want. Then just copy the file over. It will probably be overwritten during the next startup, but until that it should work.

This seems to be frequently discussed as a general Docker-related topic. Based on the discussions I briefly checked, I would say that the most convenient way is to run a local DNS cache on host and configure it to obey /etc/hosts.

If you use NetworkManager, you can tell it to run dnsmasq for that purpose. Set dns=dnsmasq in NetworkManager.conf (if it does not do that already) and create /etc/NetworkManager/dnsmasq.d/hosts.conf with addn-hosts=/etc/hosts as the content.

Similar configuration can be achieved with other network managers I believe.

Alternatively, you may run dnsmasq manually and add 127.0.0.1 to your /etc/resolv.conf.