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
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.
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.
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.
See @Thomas answer:
/etc/hosts is now writable as of Docker 1.2.
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
/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With