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-hostas documented by Asad in stackoverflow.com/a/55656083/869951 is next best solution 100%. Usingdocker build --add-hostwill NOT solve the problem.