如何在容器中解析docker主机名(/ etc / hosts) [英] How to resolve docker host names (/etc/hosts) in containers

查看:607
本文介绍了如何在容器中解析docker主机名(/ etc / hosts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析容器中Docker主机的/ etc / hosts中定义的名称?
在我的Docker主机中运行的容器可以解析公共名称(例如www.ibm.com),因此Docker dns可以正常工作。
我想从容器中解析Docker主机名称(例如127.17.0.1 smtp)。

how is it possible to resolve names defined in Docker host's /etc/hosts in containers? Containers running in my Docker host can resolve public names (e.g. www.ibm.com) so Docker dns is working fine. I would like to resolve names from Docker hosts's (e.g. 127.17.0.1 smtp) from containers.

我的最终目标是连接到在Docker主机中运行的服务(例如smtp服务器)中的容器。我知道我可以从容器中使用Docker主机IP(127.17.0.1),但我认为Docker也会使用Docker主机/ etc / hosts来构建容器的解析文件。

My final goal is to connect to services running in Docker host (e.g. smtp server) from containers. I know I can use the Docker Host IP (127.17.0.1) from containers, but I thought that Docker would have used the Docker host /etc/hosts to build containers's resolve files as well.

我什至很确定我已经看过一段时间了……但是我可能错了。

I am even quite sure I have seen this working a while ago... but I could be wrong.

有什么想法吗?

乔瓦尼

推荐答案

查看-add-host docker 命令的标志: https://docs.docker.com/engine/reference/run/#managing-etchosts

$ docker run --add-host = smtp:127.17.0.1容器命令

在Docker中, / etc / hosts 不能在运行时被覆盖或修改(安全功能)。您需要使用Docker的API,在本例中为-add-host 来修改文件。

In Docker, /etc/hosts cannot be overwritten or modified at runtime (security feature). You need to use Docker's API, in this case --add-host to modify the file.

对于 docker-compose ,使用 extra_hosts 选项。

整个连接到主机中运行的服务问题,请参阅此GitHub问题中的讨论: https:/ /github.com/docker/docker/issues/1143

For the whole "connect to services running in host" problem, see the discussion in this GitHub issue: https://github.com/docker/docker/issues/1143.

解决此问题的常用方法是使用-add -host ,其中包含主机的Docker网关地址,例如-add-host = dockerhost:172.17.42.1 。在上面的问题中检查某些脚本,这些脚本可以找到正确的IP并启动您的容器。

The common approach for this problem is to use --add-host with Docker's gateway address for the host, e.g. --add-host="dockerhost:172.17.42.1". Check the issue above for some scripts that find the correct IP and start your containers.

这篇关于如何在容器中解析docker主机名(/ etc / hosts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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