简体   繁体   English

如何绑定VM docker-machine创建OSX IP地址?

[英]How to bind the VM docker-machine creates to OSX IP address?

I'm developing locally with Docker on OSX using the latest Docker toolkit. 我正在使用最新的Docker工具包在OSX上使用Docker在本地开发。 I have a node server running in a docker container, bound to port 9999 of the VM. 我有一个在docker容器中运行的节点服务器,绑定到VM的端口9999。 I can hit this server from a browser on my mac, and I would like to hit from another device on the same network. 我可以从我的Mac上的浏览器中点击这个服务器,我想从同一网络上的另一台设备上攻击。 Is there a way to bind the VM to the machine's IP address? 有没有办法将VM绑定到机器的IP地址? Or otherwise expose it? 或以其他方式揭露它?

I just figured this one out today! 我今天刚想出这个! I am using docker-machine with virtualbox on Mac OS 10.10.5. 我在Mac OS 10.10.5上使用带有virtualbox的docker-machine。 The first thing I tried was to change the network interface from NAT to Bridged. 我尝试的第一件事是将网络接口从NAT更改为Bridged。 This just breaks docker-machine's ability to communicate with the VM. 这只会破坏docker-machine与VM通信的能力。

Instead I ADDED another network adapter running in bridged mode. 相反,我添加了另一个在桥接模式下运行的网络适配器。 After starting the docker-machine I get this: 启动docker-machine后我得到了这个:

$ docker-machine ip redis-test
10.222.11.242

That is a local network address accessible from anyone else in my office or on my VPN. 这是我的办公室或VPN上的其他任何人都可以访问的本地网络地址。

Then if I run something like: 然后,如果我运行如下:

$ docker run -p 6379:6379 -d redis

I get a containerized redis service running on port 6379 of the 10.222.11.242 address. 我得到一个在10.222.11.242地址的端口6379上运行的容器化redis服务。

So I can do this from anywhere else on the network: 所以我可以从网络上的任何其他地方做到这一点:

$ telnet 10.222.11.242 6379
Trying 10.222.11.242...
Connected to 10.222.11.242.
Escape character is '^]'.
info
$1827
# Server
redis_version:2.8.19
...

And as a cool bonus of this we can remap the ports like this: 作为一个很酷的奖金,我们可以像这样重新映射端口:

$ docker run -p 8080:6379 -d redis
e7cc53d9c157a658041c3bee5967dd3678b4d35e6146a02220a87bfebfc919ad
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
e7cc53d9c157        redis               "/entrypoint.sh redis"   7 seconds ago       Up 6 seconds        0.0.0.0:8080->6379/tcp             goofy_yonath
bf1dc6c7c6b5        redis               "/entrypoint.sh redis"   51 minutes ago      Up 51 minutes       0.0.0.0:6379->6379/tcp             redis

Now I have two redis instances listening on different ports (6379 and 8080) of the same IP. 现在我有两个redis实例侦听同一IP的不同端口(6379和8080)。

EDIT: Here are some details to help those confused about adding a NIC to the VM in VirtualBox. 编辑:这里有一些细节,以帮助那些在VirtualBox中添加NIC到VM的困惑。 I have only used VirtualBox for this and cannot advise about other virtualization system configurations. 我只使用了VirtualBox,并且无法建议其他虚拟化系统配置。

  1. Stop the VM by selecting it in the VM Manager and using the right-click menu or pressing 'command-F'. 通过在VM Manager中选择VM并使用右键单击菜单或按“command-F”来停止VM。
  2. Click "Settings". 单击“设置”。
  3. Click "Network". 点击“网络”。
  4. Select one of the Adapters that is not currently enabled. 选择当前未启用的其中一个适配器。
  5. Enable it. 启用它。
  6. Select "Bridged Adapter" in the "Attached to" selection. 在“附加到”选项中选择“桥接适配器”。
  7. Click OK. 单击确定。
  8. Start your VM and try it out. 启动您的VM并试用它。

NOTE: I am sure there are some clever command line options for doing this setup, but since I only ever needed to set it once I have never bothered to automate it. 注意:我确信有一些聪明的命令行选项可用于执行此设置,但因为我只需要设置它一次我从未打扰自动化它。

You could setup port forwarding on the VirtualBox NAT adaptor. 您可以在VirtualBox NAT适配器上设置端口转发。

Bridging it to the local network (in the answer above) is not the same as using the OSX IP address. 将其桥接到本地网络(在上面的答案中)与使用OSX IP地址不同。 Bridging can sometimes cause extra headaches if you are on laptop and move to different internet connections. 如果您使用笔记本电脑并转移到不同的互联网连接,桥接有时会导致额外的麻烦。 The VM may not automatically pull a new IP from the new network, etc. VM可能不会自动从新网络中提取新IP等。

In the UI go to Settings --> Network --> Port Forwarding or from the commandline something like this: 在UI中,转到设置 - >网络 - >端口转发或从命令行输入如下内容:

VBoxManage controlvm "default" natpf1 "tcp-port9999,tcp,,9999,,9999";

where "default" is the name of the VM ("default" is normally used for docker-machine) and 9999 is the port you want to map. 其中“default”是VM的名称(“默认”通常用于docker-machine),9999是您要映射的端口。

More info at: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md 有关更多信息, 访问: https//github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

https://www.virtualbox.org/manual/ch06.html (Configuring Port forwarding with NAT) https://www.virtualbox.org/manual/ch06.html (使用NAT配置端口转发)

你把expose命令放在Dockerfile中了吗?

EXPOSE 9999

Based on @e.thompsy answer, here is the command line version. 根据@ e.thompsy的答案,这是命令行版本。 First, figure out the interface name you need using: 首先,找出您需要使用的接口名称:

vboxmanage list bridgedifs

For me it was en1. 对我来说这是en1。 Then: 然后:

docker-machine create ...

docker-machine stop $VM

vboxmanage modifyvm "$VM" --nic3 bridged --bridgeadapter3 en1

docker-machine start $VM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM