Editable /etc/hosts and /etc/resolv.conf in Docker container

Claim:
sometimes we need add ability to change /etc/hosts and /etc/resolv.conf in your docker container. Ex. PulseSecure rewrites defaults hosts and resolv.conf files with his files.

Solution:
First you must run docker container with --privileged key. In docker-compose it looks like:
service:
build:
context: ./docker/app
dockerfile: Dockerfile
privileged: true

In the docker container you must umount current /etc/hosts with below instructions in your Dockerfile:
RUN umount /etc/hosts && echo '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' | tee /etc/hosts

Don't forget create your hosts file. If you will not create it this can call other issues.

Now you can edit /etc/hosts in the container as you wish. Lets do same things for the /etc/resolv.conf
RUN umount /etc/resolv.conf && echo 'nameserver 127.0.0.11\
options ndots:0' | tee /etc/resolv.conf

Here we umounting /etc/resolv.conf and adding ours. Nameserver by default must be 127.0.0.11.

Thats all. Now you can edit your /etc/hosts and /etc/resolv.conf files as you wish without any restrictions.

By the way if you catch an error with umount: /etc/hosts: must be superuser to umount try to do same things in yours entrypoint file. For example:
#!/bin/bash

#Preparing system for the pulse secure
su -c 'umount /etc/hosts' && echo '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' | tee /etc/hosts

su -c 'umount /etc/resolv.conf' && echo 'nameserver 127.0.0.11
options ndots:0' | tee /etc/resolv.conf

/usr/bin/supervisord -c /etc/supervisord.conf

Leave a Reply

Your email address will not be published. Required fields are marked *