Docker Community Forums

Share and learn in the Docker community.

Propagate /etc/hosts between containers added to same network


(Aelg) #1

Hi everyone,

I’m toying with the new network feature from docker 1.9 and I’m wondering how /etc/hosts is managed now. Here’s my use case :
I create a bridge network with

docker network create --driver bridge my_bridge

And add a container in it specifying its hostname

docker run -d --net my_bridge -h container_A -t bla:new /sbin/init 3 

I can see an /etc/hosts entry for the hostname I specified in the container, fine.

When I add a second container in the same bridge

docker run -d --net my_bridge -h container_B -t bla:new /sbin/init 3

I’d like to have container_A and container_B in both /etc/hosts’s containers.
Is there a flag or something to allow that ? I didn’t see any so far.

If there’s not, is there a reason not to have this functionality ?
Otherwise I’d be happy to contribute.

(Mohammedazfar2015) #2


I tried your attempt in single host …
you can try to find the corresponding docker driver like "docker network ls"
Im seeing all containers entries are in docker network inspect < network id >

Also you can see hostconfig.json for container mapped with related network… im able to see host file entry in each containers… ( /etc/hosts ) But i used the option --name in docker run … instead of -h . Can you give a try and let me know if you need help

(Aelg) #3

Thanks for your answer !

Indeed I can see /etc/hosts entries for containers in the same network when I use the --name option, but the container’s own name does not appear in it’s /etc/hosts file, I have to add a -h to have container’s hostname entrie.

The drawback I see when using --name vs -h option is that I won’t be able to use the same hostname for multiple containers, even if there are in different networks.

Also there is the --mac-address option, now it applies to the docker run command, but wouldn’t it be better to set the mac address when calling the docker network connect command ?
And the same for hostnames, if one wants to have containers in multiple networks, and have different hostnames for each attached interface ?
Something like :
docker network connect --hostname containerAbrA --mac-address aa:bb:cc:dd:ee:ff brA 3c08024a0547
docker network connect --hostname containerAbrB --mac-address aa:bb:cc:dd:ee:ff brB 3c08024a0547