从位于boot2docker vm中的Docker容器访问主机作为localhost [英] Accessing host machine as localhost from a Docker container that's also inside a boot2docker vm

查看:169
本文介绍了从位于boot2docker vm中的Docker容器访问主机作为localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一台服务器在OSX上的端口8000上运行。我的Docker容器如何通过 localhost:8000 访问它?我不能更改主机名,因为容器中的应用程序不在我的控制之内。



我已经阅读过以前关于使用 --net =host用于容器访问主机的网络。但是,我在OSX上,Docker在VM中运行,所以从Docker容器的 localhost 中, - net =host VM,而不是我真正的机器。



然后我尝试了端口转发解决方法如下所示: VBoxManage modifyvmboot2docker-vm--natpf1tcp-port8000,tcp ,, 8000,,8000;



任何建议将不胜感激。

解决方案

感谢palimpestor的答案,我想出了:



而不是 - net =host ,使用 - add-host =localhost:10.0.2.2



确实: / p>


  • 10.0.2.2是以NAT模式为访客网络接口定义的默认网关(参考)。

    阅读:这是你的主持人,se zh-cn从boot2docker。

  • - 添加主机... 正在添加 localhost 10.0 / etc / hosts中的.2.2 reference < a>)



注意:您需要在boot2docker VM VirtualBox设置中设置NAT适配器(我通过GUI ,不知道CLI)。


Suppose I have a server running on port 8000 on OSX. How can my Docker container access it via localhost:8000? I can't change the hostname too as the app in the container is not in my control.

I've read this previous discussion on using --net="host" for a container to access the host machine's network. However, I'm on OSX and Docker runs inside a VM so localhost from the Docker container with --net="host" goes to the VM and not my real machine.

Then I tried port forwarding workaround like so: VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000"; to no avail.

Any suggestion would be greatly appreciated.

解决方案

Thanks to palimpestor's answer I figured it out:

Instead of --net="host", use --add-host="localhost:10.0.2.2"

Indeed:

  • 10.0.2.2 is the default gateway defined for the guest network interface in NAT mode (reference).
    Read: it's your host, seen from boot2docker.
  • --add-host... is adding localhost 10.0.2.2 in /etc/hosts (reference)

Note: you need to have set up a NAT adapter in your boot2docker VM VirtualBox settings (I did it through the GUI, don't know the CLI).

这篇关于从位于boot2docker vm中的Docker容器访问主机作为localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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