gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:02:23 25 4
gpt4 key购买 nike

我有一台主机,其/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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com