How can I make /etc/hosts writable by root in a Do

2019-03-09 02:26发布

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

标签: docker
4条回答
冷血范
2楼-- · 2019-03-09 02:37

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.

查看更多
▲ chillily
3楼-- · 2019-03-09 02:38

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

查看更多
欢心
4楼-- · 2019-03-09 02:40

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.

查看更多
太酷不给撩
5楼-- · 2019-03-09 02:45

/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.

查看更多
登录 后发表回答