Docker容器 - 如何配置,以便在流氓软件中运行时获得可行的IP地址? [英] Docker container - how to configure so it gets a viable IP address when running in vagrant?

查看:186
本文介绍了Docker容器 - 如何配置,以便在流氓软件中运行时获得可行的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker(www.docker.io)看起来很棒。然而,在安装VirtualBox之后,Vagrant
...最后在Mac上的Docker,我发现不可能从另一台计算机(或从Mac上的终端会话)访问在Docker容器中运行的服务, 。我试图访问的服务是Redis。



问题似乎是没有分配给Docker容器的IP地址的路由。在这种情况下,容器的IP是172.16.42.2,而Mac的IP是196.168.0.3。



一对情侣注释:


  1. 可以访问它 - 但只能从VirtualBox会话中。这可以使用redis-cli -h 172.16.42.2 -p 6379.

  2. 我已经将config.vm.network:bridged添加到VagrantFile中以试图获取,但是没有解决问题。


解决方案

由vagrant生成的虚拟机确实是孤立的为了从主机访问它,您可以为其分配专用网络。
尝试 config.vm.network:private_network,ip:192.168.50.4,而不是执行 config.vm.network:bridged ,它应该做的伎俩



但是,这将只允许您访问VM本身,而不是容器。

为了这样做,运行容器时,可以添加 -p 选项



ex: docker run -d -p 8989 base nc -lkp 8989



将在容器内运行一个在8989上侦听的netcat,并公开暴露该端口。由于它也使用-d运行,容器将处于分离模式,唯一的输出将是容器的ID



为了暴露端口,Docker做一个简单的NAT为了了解真实的端口,您可以



do docker port<容器的ID> 8989



Netcat将从mac在192.168.50.4:<result>


Docker (www.docker.io) looks terrific. However, after installing VirtualBox, Vagrant ... and finally Docker on a Mac, I'm finding it's not possible to access the service running in the Docker container from another computer (or from a terminal session on the Mac). The service I'm trying to access is Redis.

The problem appears to be that there's no route to the IP address assigned to the Docker container. In this case the container's IP is 172.16.42.2 while the Mac's IP is 196.168.0.3.

A couple notes:

  1. It IS possible to access it - but only from within the VirtualBox session. This can be done using redis-cli -h 172.16.42.2 -p 6379.
  2. I have added "config.vm.network :bridged" to the VagrantFile in an attempt to get the, but that didn't solve the problem.

解决方案

The VM generated by vagrant is indeed isolated, in order to access it from your host, you can allocate a private network to it. Instead of doing config.vm.network :bridged, try config.vm.network :private_network, ip: "192.168.50.4", It should do the trick

However, this will only allow you to access the VM itself, not the containers.
In order to do so, when running the container, you can add the -p option

ex: docker run -d -p 8989 base nc -lkp 8989

This will run a netcat listening on 8989 within a container and expose the port publicly. As it is also run with -d, the container will be in detached mode and the only output will be the container's ID

In order to expose the port, Docker do a simple NAT. In order to know the real port, you can

do docker port <ID of the container> 8989

Netcat will be available from the mac at 192.168.50.4:<result>

这篇关于Docker容器 - 如何配置,以便在流氓软件中运行时获得可行的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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