Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit /etc/hosts file in running docker container

Tags:

docker

centos7

I am working on an application which requires some configuration to be stored in /etc/hosts file of a docker container. I have tried it with many options but did not find any correct way of modifying the /etc/hosts file at run time.

I want to do it either by Dockerfile or by java code. I am able to build the docker image and modify the /etc/hosts files manually, but unfortunately that is not our projects requirement.

like image 253
Vinay Pandey Avatar asked Jun 16 '16 13:06

Vinay Pandey


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 I edit files in docker container?

Unfortunately, this also means Docker containers don't have a file editor like Vim or Nano preinstalled. In this guide, we'll show you how to install an editor, make the changes you need to, and return the container to its original state, both from the command line and using the Docker extension inside VS Code.

Can you edit code in docker container?

You can now make changes inside the container, without manually copying files or setting up a working directory bind mount. This maximizes efficiency when using a Dockerized development environment or debugging a malfunctioning container.


3 Answers

The recommended solution is to use the --add-host option to docker run or the equivalent in the docker-compose.yml file if you're using docker-compose.

BUT, I was in the same boat as you. I have a script that modifies the hosts file that I wanted to run in the container, so what I did was COPY the script into the container and make it executable, then in the Dockerfile's CMD script that your choose, call your script to modify the hosts file

Works

in Dockerfile

# add the modifyHostsFile script, make it executable
COPY ./bash-scripts/modifyHostsFile.sh /home/user/modifyHostsFile.sh
RUN sudo chmod +x /home/user/modifyHostsFile.sh


# run the script that starts services
CMD ["/bin/bash", "/home/user/run.sh"]

And in the run.sh script I execute that script to modify the hosts file

# modify the hosts file
bash ./modifyHostsFile.sh

Doesn't Work

in Dockerfile

# add the modifyHostsFile script, make it executable
COPY ./bash-scripts/modifyHostsFile.sh /home/user/modifyHostsFile.sh
RUN sudo chmod +x /home/user/modifyHostsFile.sh

# modify the hosts file right now
RUN bash /home/user/modifyHostsFile.sh    

# run the script that starts services
CMD ["/bin/bash", "/home/user/run.sh"]

You have to run the script that modifies your hosts file during your CMD script. If you run it via RUN bash ./modifyHostsFile.sh in your Dockerfile it will be added to that container, but then Docker will continue to the next step in the Dockerfile and create a new container (it creates a new intermediary container for each step in the Dockerfile) and your changes to the /etc/hosts will be overridden.

like image 98
ejfrancis Avatar answered Nov 08 '22 10:11

ejfrancis


Depends upon what sort of modifications you want to do. If you just need to add more hosts, you can probably do it within docker run command like -

docker run --add-host="localA:127.0.0.1" --add-host="localB:172.0.0.1" ....

This link might be useful as well :- https://github.com/docker/docker/issues/10324

like image 41
Avichal Badaya Avatar answered Nov 08 '22 08:11

Avichal Badaya


I had the same problem and overcome it with vi in ex mode.

ex -sc '%s/foo/bar/g|x' /etc/hosts

like image 1
Govind Kailas Avatar answered Nov 08 '22 08:11

Govind Kailas