4

在 Docker 容器中访问主机

 2 years ago
source link: https://rabbit52.com/2018/05/access-host-from-docker-container/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在 Docker 容器中访问主机

发表于

2018-05-04

|

更新于 2020-05-23

| 分类于 Devel

有时候多个容器不是在一个 compose 中启动的,不方便 link 起来或者是有些服务是直接跑在主机上的,所以从容器访问主机的场景十分普遍,我们需要一个类似 127.0.0.1 的 IP 地址能在容器中访问到主机。在 linux 环境中这个问题很好解决,安装完 docker 之后会出现一个 docker0 的网卡,这个网卡上绑定的 IP 就可以在容器中访问,并指向主机,默认情况下会是 172.17.0.1

679b427egy1fqzl4fishlj20ed03q758.jpg

但是 mac 和 windows 开发环境下不会出现这个网卡也没有这样一个 IP 来访问,不过最新的 docker 18.03 加入了一个 feature,在容器中可以通过 host.docker.internal hostname 来访问主机,解析得到的 ip 是 192.168.65.2 正是 docker 的设置中的子网网段

679b427egy1fqzlf2c3yhj20dw0ctwfl.jpg

详情参考官方文档:
https://docs.docker.com/docker-for-mac/networking/#there-is-no-docker0-bridge-on-macos


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK