美文网首页
Docker初学者问题 - 如何在Docker容器中访问主机(H

Docker初学者问题 - 如何在Docker容器中访问主机(H

作者: 啤酒沫 | 来源:发表于2019-01-14 20:24 被阅读0次

    这个问题之前查过网上的几种方法,基本上的思路是在运行时的主机的IP地址然后在容器中添加一个Host映射。但我使用这种方法时遇到主机需要经常切换不同的网络,这时主机的IP地址变得不可靠了。
    后来认真阅读了docker的官方文档,发现有一个非常简单的方法。使用host.docker.internal即可访问Host
    官方内容如下

    我想要从容器连接主机的一个服务

    当主机需要变化IP地址(或者它根本就没有网络)。在18.03 之后的版本中我们建议使用一个专门的DNS名称host.docker.internal,这个DNS名称将被解析到主机的内部IP。 这只是为了开发目的,不要用于非Mac版Docker的生产环境。

    另:网关可以通过gateway.docker.internal访问。

    原文 Docker for Mac

    I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST

    The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.

    The gateway is also reachable as gateway.docker.internal.

    根据上述内容我们需要注意的是:
    1、仅限于Docker for Mac的环境。
    2、不要在生产环境上使用这种方法。

    相关文章

      网友评论

          本文标题:Docker初学者问题 - 如何在Docker容器中访问主机(H

          本文链接:https://www.haomeiwen.com/subject/fsajdqtx.html