Insert Docker parent host ip into container's

2020-03-11 07:10发布

问题:

I'm new to Docker and trying to understand what is the best way to insert docker parent host ip into container hosts file.

I'm using the following command in my Dockerfile

RUN /sbin/ip route|awk '/default/ { print $3,"\tdockerhost" }' >> /etc/hosts

but sometimes hosts ip get change so its non relevant anymore...

The reason to do that, if you ask yourself, is that i need to access another 2 dockers containers (and link not offer this feature).

Thanks,

回答1:

The --add-host option is made for this. So, in your docker run command, do something like:

docker run --add-host dockerhost:`/sbin/ip route|awk '/default/ { print  $3}'` [my container]


回答2:

--add-host option can be used when you create/run your container but since /sbin/ip command isn't available in operating systems like OSX, we can use a more generic solution:
docker run --add-host=dockerhost:`docker network inspect \ --format='{{range .IPAM.Config}}{{.Gateway}}{{end}}' bridge` [IMAGE]



标签: docker