docker的“net = host”设置如何工作,我如何做类似的例子。 VirtualBox的? [英] How does docker's `net=host` setting work, and how can I do something similar with e.g. VirtualBox?

查看:147
本文介绍了docker的“net = host”设置如何工作,我如何做类似的例子。 VirtualBox的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker有一个运行选项 net = host 记录在这里,允许您运行与主机共享网络堆栈的虚拟机 - 例如,docker容器内的进程可以通过 localhost ,反之亦然。



我想在Mac OS X上设置一个同样的东西的Linux虚拟机;我已经尝试使用Vagrant及其各种网络设置,没有多少运气。



Docker的VM是否依赖主机和客户机操作系统都是Linux,还是有一些方法完成这个我所缺少的OSX-> Linux?

解决方案

感谢我的同事的一些帮助,我找到了一个解决方案问题。此解决方案适用于boot2docker / VirtualBox。我刚刚使用 boot2docker init 创建了Docker VM,我没有对VM配置做出任何具体的更改。



首先,您使用 - net = host 运行docker映像,以便与主机VM共享网络,例如



$ $ $ $ $ $ $ $ $ $












然后,您需要从用于docker容器的VM中找到IP地址,您可以通过运行OSX主机的 boot2docker ssh 然后您可以通过查找虚拟机的网关来查找虚拟机的IP地址:

  $ netstat -rn | grep UG | awk'{print $ 2}'
10.0.2.2

所以在我的情况下是10.0。 2.2。您现在可以使用该IP地址访问在主机上打开的端口,即在通过Docker容器运行在OSX上的程序。



要自动化,您可以先找到IP地址,然后将其作为环境变量传递给docker命令...


Docker has a run option net=host documented here that allows you to run a virtual machine that shares the network stack with the host — for example, processes inside the docker container can connect to the host machine via localhost and vice versa.

I want to set up a Linux VM on Mac OS X that does the same thing; I've tried using Vagrant and its various networking settings without much luck.

Does Docker's VM rely on the host and guest OSes both being Linux, or is there some way to accomplish this OSX->Linux that I'm missing?

解决方案

Thanks to some help from my colleagues I found a solution to this problem. This solution works with boot2docker/VirtualBox. I just created my docker VM with boot2docker init, I didn't make any specific changes to the VM configuration.

First you run the docker image with --net=host, so that it shares the network with the host VM e.g.

docker run -it --net=host ubuntu bash

Then you need to find the IP address from the VM used for the docker containers, you can do this by running boot2docker ssh the OSX host. You can then find the IP address of the VM by finding its gateway:

$ netstat -rn | grep UG | awk '{print $2}'
10.0.2.2

So in my case it's 10.0.2.2. You can now access ports opened on the host, i.e. on a program running on OSX from your docker container by using this IP address.

To automate you could find the IP address first and then pass it into the docker command as an environment variable...

这篇关于docker的“net = host”设置如何工作,我如何做类似的例子。 VirtualBox的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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