Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make /etc/hosts writable by root in a Docker Container?

Tags:

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

like image 249
user2888205 Avatar asked Oct 16 '13 21:10

user2888205


People also ask

How do I edit a ETC host in a container?

Generally speaking, /etc/hosts file can not be modified before running the docker container. However, current docker has an option “–add-host” which adds host-entries onto /etc/hosts when the container is run.

Can Docker container write to host?

This allows for direct access to the host file system inside of the container and for container processes to write directly to the host file system. This I/O allows for Docker images to be used for specific tasks that may be difficult to do with the tools or software installed on only the local host machine.

Where is docker host file?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem.


2 Answers

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 
like image 153
aledbf Avatar answered Oct 19 '22 09:10

aledbf


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

like image 36
Thomas Avatar answered Oct 19 '22 09:10

Thomas