23

I have a simple Dockerfile but the first RUN command (to append a host IP address to /etc/hosts) has no effect

FROM dockerfile/java
RUN sudo echo "XX.XX.XXX.XXX some.box.com MyFriendlyBoxName" >> /etc/hosts
ADD ./somejavaapp.jar /tmp/
#CMD java -jar /tmp/somejavaapp.jar
EXPOSE 8280

I build using

docker build .

and then test the RUN echo line has worked using

sudo docker run -t -i <built image ID> /bin/bash

I am then into the container but the /etc/hosts file has not been appended. Running the same echo .... line while now in the container has the desired effect

Can anyone tell me what is wrong with my dockerfile RUN ...?

Rob McFeely
  • 2,823
  • 8
  • 33
  • 50
  • 4
    Within Docker `/etc/hosts` is made at runtime (when you use `docker run`). Other people have used dnsmasq within the container to have additional entries. From 1.2 you can edit `/etc/hosts` after runtime, however it is not saved in commits, and it can't be done from a Dockerfile for this reason. – Marcus Hughes Dec 08 '14 at 11:58
  • Thanks Kevs answer worked for me but ill keep this in mind. cheers – Rob McFeely Dec 09 '14 at 17:28

4 Answers4

39

Docker will generate /etc/hosts dynamically every time you create a new container. So that it can link others. You can use --add-host option:

docker run --add-host www.domain.com:8.8.8.8 ubuntu ping www.domain.com
Bert
  • 2,134
  • 19
  • 19
kev
  • 155,172
  • 47
  • 273
  • 272
  • Bingo that worked great. Thanks for the explaination – Rob McFeely Dec 09 '14 at 17:29
  • This is great but I can't seem to find this option in the API (1.15). – marcinx Dec 31 '14 at 09:02
  • 3
    To have multiple names on one IP try having the names enclosed in double quotes: `--add-host "www.domain.com www.otherdomain.com":8.8.8.8` – Stephane Sep 05 '16 at 08:40
  • What if I need this when building, --add-host is not an option for build (docker 1.12.1), `RUN echo "name ip">>/etc/hosts && make` only works for root – MortenB Oct 11 '16 at 09:00
  • @MortenB fyi it has been added to docker build now. but note it's only during the build, not saved to the image. – scipilot Feb 16 '18 at 01:36
4

If you use docker-compose, use extra_hosts:

extra_hosts:
  - "somehost:162.242.195.82"
  - "otherhost:50.31.209.229"
andi
  • 912
  • 1
  • 10
  • 25
1

If you are trying to maintain the hosts file entries between host machine and container another way would be to wrap your command with a shell script that maps your hosts' /etc/hosts into --add-host params:

~/bin/java:

#!/bin/sh

ADD_HOSTS=$(tail -n +10 /etc/hosts | egrep -v '(^#|^$)' | sed -r 's/^([a-z0-9\.\:]+)\s+(.+)$/--add-host="\2:\1"/g')

eval docker run \
    -it \
    --rm \
    $ADD_HOSTS \
    <image> \
    java $*

return $?

Obviously replace java with whatever you are trying to do...

Explanation; ADD_HOSTS will take everything after the first 10 lines in your hosts' /etc/hosts file | remove comments and blank lines | re-order the entries into --add-host params.

The reason for taking everything after the first 10 lines is to exclude the localhost and ipv6 entries for your host machine. You may need to adjust this to suit your needs.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
kralos
  • 23
  • 3
0

I had such a problem that I wanted to add multiple domain names for a single IP. Actually, there is no way to make in build time because at each run of the image the /etc/hosts will be overwritten. So, it seems that utilizing --add-host option or its extra_hosts aliens in the docker-compose method be the only way. But as a matter of fact, adding each of the domain names by a single --add-host was so messy to me. I tried below one and it works:

docker run --add-host "some.box.com MyFriendlyBoxName":X.X.X.X ubuntu cat /etc/hosts

Actually, it just copies and pastes the String after the --add-host to the colon.