With a more recent version of docker, this could be done with docker-compose and its extra_hosts
directive
Add hostname mappings.
Use the same values as the docker run
client --add-host
parameter (which should already be available for docker 1.8).
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
In short: modify /etc/hosts
of your container when running it, instead of when building it.
With Docker 17.x+, you have a docker build --add-host
mentioned below, but, as commented in issue 34078 and in this answer:
The --add-host
feature during build is designed to allow overriding a host during build, but not to persist that configuration in the image.
Those links point to strategies for dealing with the problem at hand:
- Run an internal DNS; you can set the default DNS server to use in the daemon; that way every container started will automatically use the configured DNS by default
- Use docker compose and provide a
docker-compose.yml
to your developers.
The docker compose file allows you to specify all the options that should be used when starting a container, so developers could just docker compose up to start the container with all the options they need to set.
These solutions can take advantage of using of the docker-compose
method that was suggested earlier in the answer, with its extra_hosts
directive.
docker run --add-host
as documented by Asad in https://mcmap.net/q/101386/-how-to-update-etc-hosts-file-in-docker-image-during-quot-docker-build-quot is next best solution 100%. Usingdocker build --add-host
will NOT solve the problem. – Tripos