Insert Docker parent host ip into container's

2020-03-11 06:46发布

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,

标签: docker
2条回答
虎瘦雄心在
2楼-- · 2020-03-11 07:15

--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]

查看更多
姐就是有狂的资本
3楼-- · 2020-03-11 07:28

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]
查看更多
登录 后发表回答