0

How can I pass the hosts saved in /etc/hosts into the docker image while building it.

I want to do this automatically, so that this snippet works on all server environments.

I tried this in the Dockerfile:

COPY /etc/hosts /etc/hosts

But obviously that didnt work with the following error:

COPY failed: stat /var/lib/docker/tmp/docker-builder736360024/etc/hosts: no such file or directory


I also tried to move the hosts (cp /etc/hosts /app/dir) first in my app-folder and then copying only with in the Dockerfile:

COPY hosts /etc/hosts

That didn't throw an error, but looking in a docker container, the container hosts are not the same as the hosting machine.


I also tried to build the image with the additional --add-host parameter, but that also did not work.

How can I do this correctly?

SeGa
  • 9,454
  • 3
  • 31
  • 70
  • which os you are on? try to use `--network=host` if its linux? – Ntwobike Jun 19 '19 at 15:15
  • Yes it's Linux.. You mean in the build command? – SeGa Jun 19 '19 at 15:24
  • AFAIK /etc/hosts is belongs to docker its not easy to update. u defined the network mode when u running the container, with host mode u will use the host machine network https://docs.docker.com/network/network-tutorial-host/ but it has some limitation. Check this question also https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build – Ntwobike Jun 19 '19 at 15:35
  • Why can’t you run a DNS service? (Something like Route 53 in an AWS environment; or Consul; or BIND; or ....) Manually maintaining `/etc/hosts` and keeping it in sync in multiple places is a huge hassle. – David Maze Jun 19 '19 at 17:11

0 Answers0