Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker echo to /etc/hosts not working

Dockerfile:

FROM ubuntu:latest

RUN touch /tmp/foo
RUN echo 'bar' > /tmp/foo

RUN echo '192.168.99.100 foo' >> /etc/hosts

ENTRYPOINT /bin/bash

Inside the container:

root@47040a03cbc1:/# cat /tmp/foo 
bar
root@47040a03cbc1:/# cat /etc/hosts 
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4  47040a03cbc1

Why does the first echo to /tmp/foo works but the second to /etc/hosts doesn't?

like image 440
kev Avatar asked Dec 19 '22 17:12

kev


2 Answers

Docker manages /etc/hosts. It does this to make container linking work. You can ask docker to append to the hosts file when starting the container with

docker run -it --add-host foo:192.168.99.100
like image 77
Alfred Rossi Avatar answered Dec 29 '22 23:12

Alfred Rossi


If use docker composer:

  extra_hosts:
 - "foo:192.168.99.100"
 - "bar:192.168.99.101"
like image 44
Toleugazy Avatar answered Dec 29 '22 21:12

Toleugazy