24

I'm new to using docker and am configuring a container.
I am unable to edit /etc/hosts (but need to for some software I'm developing). Auto-edit (via sudo or root) of the file says its on a read only file system. Manual (vim) edit of the file says its read-only and I'm unable to save changes as root (file permissions are rw for owner (root)).

I can however modify other files and add files in /etc.

Is there some reason for this?

Can I change the Docker configuration to allow edit of /etc/hosts?

thanks

5 Answers 5

17

UPDATE 2014-09

See @Thomas answer:

/etc/hosts is now writable as of Docker 1.2.

Original answer

You can use this hack in the meanwhile

https://unix.stackexchange.com/questions/57459/how-can-i-override-the-etc-hosts-file-at-user-level

In your Dockerfile:


ADD your_hosts_file /tmp/hosts
RUN mkdir -p -- /lib-override && cp /lib/x86_64-linux-gnu/libnss_files.so.2 /lib-override
RUN perl -pi -e 's:/etc/hosts:/tmp/hosts:g' /lib-override/libnss_files.so.2
ENV LD_LIBRARY_PATH /lib-override

1
  • Don't take this the wrong way, but... I love you! Only wish I found this hack before - configuring dnsmasq really gave me some not-so-fun couple of hours
    – qkrijger
    Mar 12, 2014 at 14:55
9

/etc/hosts is now writable as of Docker 1.2.

From Docker's blog:

Note, however, that changes to these files are not saved during a docker build and so will not be preserved in the resulting image. The changes will only “stick” in a running container.

4
  • Note this is true for the current trunk version, but is not yet included in the one tagged 1.2.0, which is currently distributed as a package.
    – relet
    Sep 22, 2014 at 10:49
  • 1
    The changelog you link to shows that it is in 1.2, and I can definitely write to /etc/hosts with a Docker 1.2 I have installed from a package.
    – Thomas
    Sep 22, 2014 at 19:58
  • 1
    Ah, sorry, I did not see it was a two-staged commit. I was checking for the --add-hosts option being present on commandline, which arrived later.
    – relet
    Sep 23, 2014 at 10:16
  • is this useful for something? If we want to update host dns mapping for the container it doesnt help that those changes get tossed out at the end. Its actually more confusing since it doesnt throw an error.
    – George
    Nov 5, 2021 at 21:16
4

This is currently a technical limitation of Docker, and is discussed further at https://github.com/dotcloud/docker/issues/2267.

It will eventually be lifted.

For now, you need to work around it, e.g. by using a custom dnsmasq server.

1

I have recently stumbled upon a need to add an entry into /etc/hosts file as well (in order to make sendmail working).

I ended up making it part of the Dockerfile's CMD declaration like this:

CMD echo "127.0.0.1 noreply.example.com $(hostname)" >> /etc/hosts \
    && sendmailconfig \
    && cron -f

So it effectively is not a part of the image, but it is always available after creating a container from the image.

0

You could do that without change your /etc/hosts file. Just add extra_hosts into your docker-compose.yml like this example bellow:

myapp:
  image: docker.io/bitnami/laravel:9
ports:
  - 80:8080
extra_hosts:
  - "myapp.test:0.0.0.0"
  - "subdomain.myapp.test:0.0.0.0"

References:

How can I add hostnames to a container on the same docker network?

Not the answer you're looking for? Browse other questions tagged or ask your own question.