Docker directly manages the /etc/hosts
files in containers. You can't bind-mount a file there.
Hand-maintaining mappings of host names to IP addresses in multiple places can be tricky to keep up to date. Consider running a DNS server such as BIND or dnsmasq, or using a hosted service like Amazon's Route 53, or a service-discovery system like Consul (which incidentally provides a DNS interface).
If you really need to add entries to a container's /etc/hosts
file, the docker run --add-host
option or Docker Compose extra_hosts:
setting will do it.
As a general rule, a container can't access the host's filesystem, except to the extent that the docker run -v
option maps specific directories into a container. Also as a general rule you can't directly change mount points in a container; stop, delete, and recreate it with different -v
options.