Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you make the Docker container use the host machine's '/etc/hosts' file?

Tags:

docker

I want to make it so that the Docker container I spin up use the same /etc/hosts settings as on the host machine I run from. Is there a way to do this?

I know there is an --add-host option with docker run, but that's not exactly what I want because the host machine's /etc/hosts file may be different on different machines, so it's not great for me to hardcode exact IP addresses/hosts with --add-host.

like image 500
d3ming Avatar asked Aug 18 '15 17:08

d3ming


People also ask

Does Docker use etc hosts?

Docker maps the host's /etc/hosts into the container at startup. Hence you can add the static mapping in your host's /etc/hosts, you can add some lines to /etc/hosts from command line for docker run with --add-host="..." , or you can derive a new image with an ENTRYPOINT , that changes the file.

How will you add a new host in etc hosts when a Docker container is run?

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 access host files?

Docker provides two ways for containers to save files on the host system so that the files are persistent even after the container is shut down. These are Docker volumes and bind mounts. This blog will teach you how to share data between a Docker containerized application and the host computer.

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


2 Answers

Use --network=host in the docker run command. This tells Docker to make the container use the host's network stack. You can learn more here.

like image 98
d3ming Avatar answered Oct 19 '22 01:10

d3ming


Add a standard hosts file -

docker run -it ubuntu cat /etc/hosts

Add a mapping for server 'foo' -

docker run -it --add-host foo:10.0.0.3 ubuntu cat /etc/hosts

Add mappings for multiple servers

docker run -it --add-host foo:10.0.0.3 --add-host bar:10.7.3.21 ubuntu cat /etc/hosts

Reference - Docker Now Supports Adding Host Mappings

like image 32
Vintesh Avatar answered Oct 19 '22 00:10

Vintesh