从同样位于 boot2docker vm 中的 Docker 容器以本地主机身份访问主机
Accessing host machine as localhost from a Docker container that's also inside a boot2docker vm
假设我在 OSX 的端口 8000 上有一个服务器 运行。我的 Docker 容器如何通过 localhost:8000
访问它?我也无法更改主机名,因为容器中的应用程序不在我的控制范围内。
我已经阅读了之前关于使用 --net="host"
让容器访问主机网络的讨论。但是,我在 OSX 上并且 Docker 在 VM 内运行,因此 localhost 从 Docker 容器和 --net="host"
转到VM 而不是我的真机。
然后我试了 port forwarding workaround 像这样:VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";
没有用。
如有任何建议,我们将不胜感激。
而不是 运行 --net="host"
,尝试 --add-host="localhost:192.168.59.3"
,这是 boot2docker 主机 IP。
感谢 palimpestor 的回答,我明白了:
而不是--net="host"
,使用--add-host="localhost:10.0.2.2"
确实:
- 10.0.2.2 是在 NAT 模式下为访客网络接口定义的默认网关 (reference)。
阅读:这是你的主机,从boot2docker看到。
--add-host...
在 /etc/hosts 中添加 localhost 10.0.2.2
(reference)
注意:您需要在 boot2docker VM VirtualBox 设置中设置 NAT 适配器(我是通过 GUI 完成的,不知道 CLI)。
如果我理解你的意图,它是:从容器连接到主机端口
截至 18.3,docker 团队已为我们提供服务:
我们使用别名 host.docker.internal
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 Desktop for Windows.
The gateway is also reachable as gateway.docker.internal
.
假设我在 OSX 的端口 8000 上有一个服务器 运行。我的 Docker 容器如何通过 localhost:8000
访问它?我也无法更改主机名,因为容器中的应用程序不在我的控制范围内。
我已经阅读了之前关于使用 --net="host"
让容器访问主机网络的讨论。但是,我在 OSX 上并且 Docker 在 VM 内运行,因此 localhost 从 Docker 容器和 --net="host"
转到VM 而不是我的真机。
然后我试了 port forwarding workaround 像这样:VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";
没有用。
如有任何建议,我们将不胜感激。
而不是 运行 --net="host"
,尝试 --add-host="localhost:192.168.59.3"
,这是 boot2docker 主机 IP。
感谢 palimpestor 的回答,我明白了:
而不是--net="host"
,使用--add-host="localhost:10.0.2.2"
确实:
- 10.0.2.2 是在 NAT 模式下为访客网络接口定义的默认网关 (reference)。
阅读:这是你的主机,从boot2docker看到。 --add-host...
在 /etc/hosts 中添加localhost 10.0.2.2
(reference)
注意:您需要在 boot2docker VM VirtualBox 设置中设置 NAT 适配器(我是通过 GUI 完成的,不知道 CLI)。
如果我理解你的意图,它是:从容器连接到主机端口
截至 18.3,docker 团队已为我们提供服务:
我们使用别名 host.docker.internal
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 Desktop for Windows.The gateway is also reachable as
gateway.docker.internal
.