在具有桥接网络的Docker容器中映射主机的/ etc / hosts [英] Map host's /etc/hosts in a Docker container having bridge networking

查看:141
本文介绍了在具有桥接网络的Docker容器中映射主机的/ etc / hosts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在这台机器上运行我的Docker容器,配置了一个Bridge网络。
由于我不在主机网络上,我的Docker容器无法访问我的机器/ etc / hosts文件的主机定义。



不幸的是一个DNS它目前不是一个选项。



我的问题是如何利用这些定义在容器中使用桥接网络?我读到安装主机/ etc / hosts文件在容器中不是一个不错的选择,因为这是由docker deamon内部处理的。

你知道我还能如何实现吗?

解决方案

您有2个选项



docker run -v / etc / hosts:/ etc / hosts< yourimage>



该选项的问题是,您的容器主机文件将被覆盖,如果您想联系该码头网络中的任何其他服务,则会发生回火。



因此,我会做



docker运行-v / etc / hosts:/ tmp / hosts< yourimage>



并在你的图像,在这行之间做某事




cat / tmp / hosts>> / etc / hosts



a)你想过滤掉一些如localhost的行,或者使用grep $ b选择特定的行$ bb)你想确保你不要在每个容器引导上重复这个,所以写一个信号量或类似的(一个文件,检查文件)

I have a host machine with some hosts resolution defined in its /etc/hosts file.

On this machine I'm running my Docker containers configured with a Bridge network.
Since I'm not on the host network my Docker containers have no access of the hosts definitions of my machine /etc/hosts file.

Unfortunately having a DNS it is not an option at the moment.

My question is how can I make use of those definitions in my containers using bridge networking? I read mounting the hosts /etc/hosts file in the container is not a good choice since that's handled internally by the docker deamon.
Do you know how else I can achieve this?

解决方案

You have 2 options

docker run -v /etc/hosts:/etc/hosts <yourimage>

the problem with the option is, that your container hosts file is overwritten, which will backfire if you want to contact any other service in that docker-network.

Thus i would do

docker run -v /etc/hosts:/tmp/hosts <yourimage>

And use a entrypoint in your image, which does something among this lines

cat /tmp/hosts >> /etc/hosts

a) You want to filter out some lines like localhost, or select specific lines using grep b) You want to ensure you do not repeat this on every container bootstrap, so write a semaphore or similar ( a file, check the file whatever )

这篇关于在具有桥接网络的Docker容器中映射主机的/ etc / hosts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆