10

I am making a docker image using a pretty simple Dockerfile. Inside the Dockerfile I have this command:

RUN printf "192.92.13.243 www.hahaha.com \n" >> /etc/hosts

The command itself seems to be OK, since creation of the image does not halt at this point.

The issue is this: When running the image, the line that is supposed to have been inserted inside "/etc/hosts" is not there.

Now, I searched around and found out that before version 1.2 of docker, there was an issue with the hosts file inside the containers. In my case, I am using version 1.5, the latest as of this moment.

Am I missing something?

UPDATE 1:

There seem to be a lot of issues for this, both open and closed, in docker's github pages.

2 Answers 2

12

This works on docker 1.7.0

RUN echo "192.168.11.112 myhost" >> /etc/hosts && wget http://myhost

The trick is to add the hostname on the same line as you use it, otherwise the hosts file will get reset, since every RUN command starts a new intermediate container. For example, this will not work:

RUN echo "192.168.11.112 myhost" >> /etc/hosts
RUN wget http://myhost
1
  • 1
    Thanks for the insight! Although correct (I checked) and useful in general, it is of no real use to me in this case. I need the "hosts" file to be populated when the container runs.
    – dlyk1988
    Jul 28, 2015 at 10:23
9

After writing the update to my question I decided to take one more hard look at the "issues" opened in github. Turns out, a workaround has been implemented:

docker run ... --add-host='server:0.0.0.0' ...

Using the "--add-host..." argument when starting a container it is possible to modify the hosts file.

3
  • 5
    I want to be able to do this in build though. The person running the container shouldnt have to know about internal hosts. Makes zero sense!
    – samsamm777
    Dec 3, 2015 at 13:27
  • I echo @samsamm777. Wonder if there is a good way of doing that?
    – Jonathan
    Apr 6, 2017 at 10:20
  • This really is very surprising behaviour, which never is a good idea in IT systems. Jul 7, 2017 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .