Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Does container inherit /etc/hosts from docker host?

In case of I have a machine that running docker (docker host) and spin up some containers inside this docker host,

I need containers' services be able to talk to each other - container expose ports and they also need to resolve by hostname (e.g: example.com)

container A needs to talk to container B with URL: example.com:3000

I've read this article but not quite sure about "inherit" from docker host, does docker host's /etc/hosts will be appended to container's /etc/hosts that running inside docker host?

https://docs.docker.com/engine/reference/run/#managing-etchosts

How to achieve?

Does this "inherit" has any connect to type of docker container networking https://docs.docker.com/v17.09/engine/userguide/networking/ ?

like image 366
Tuyen Pham Avatar asked Aug 30 '25 16:08

Tuyen Pham


1 Answers

It does not inherit the host's /etc/hosts file. The file inside your container is updated by docker when using the --add-host parameter or extra_hosts in docker-compose. You can add individual records by using extra_hosts in docker-compose (https://docs.docker.com/compose/compose-file/#extra_hosts).

Although if you're just trying to get 2 containers talking to each other you can alternatively connect them to the same network. In docker-compose you can create what's called an external network and have all your docker-compose files reference it. you will then be able to connect by using the full docker container name (eg. http://project_app_1:3000).

See https://docs.docker.com/compose/compose-file/#external

like image 96
Michael Barany Avatar answered Sep 02 '25 05:09

Michael Barany