networking - 在具有桥接网络的 Docker 容器中映射主机的/etc/hosts

标签 networking docker dns hosts

我有一台主机,其/etc/hosts 文件中定义了一些主机解析。

在这台计算机上,我正在运行配置了桥接网络的 Docker 容器。
由于我不在主机网络上,因此我的 Docker 容器无法访问我的计算机/etc/hosts 文件的主机定义.

不幸的是,目前还没有 DNS 选项。

我的问题是如何使用桥接网络在容器中使用这些定义?我读到在容器中安装主机/etc/hosts 文件不是一个好的选择,因为这是由 docker deamon 内部处理的。
你知道我还能如何实现这一目标吗?

最佳答案

我认为最好使用 --add-host 命令行选项将条目添加到容器的 /etc/hosts 中。

这是 official Docker Reference 的摘录

Managing /etc/hosts

Your container will have lines in /etc/hosts which define the hostname of the container itself as well as localhost and a few other common things. The --add-host flag can be used to add additional lines to /etc/hosts.

$ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts
172.17.0.22     09d03f76bf2c
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
127.0.0.1       localhost
::1               localhost ip6-localhost ip6-loopback
86.75.30.9      db-static

关于networking - 在具有桥接网络的 Docker 容器中映射主机的/etc/hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41440650/

相关文章:

c - 您可以在一个套接字接收调用中获得两个数据包以进行多播吗?

windows - 如何从 tar 文件加载 Docker 镜像

docker - 使用 docker 时如何执行 ../COPY

docker - Docker Swarm收听Manager和工作节点的ISP ip

PHP查询一条SRV记录

linux - netlink 和大端格式

networking - 为什么我们需要地址解析协议(protocol)?

dns - 搜索域和子域的所有 DNS TXT 记录

ssl - 在 Jetty 环境中操纵 TLS1.2 通信的可能性

docker - 使用基本身份验证从注册表部署 docker 镜像