使用Vagrant Docker提供程序从boot2docker转发端口 [英] Forward Ports from boot2docker using the Vagrant Docker provider

查看:207
本文介绍了使用Vagrant Docker提供程序从boot2docker转发端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图利用Vagrant 1.6的Docker提供商,我似乎遇到了一个障碍。我可以成功地启动Docker容器和客户机操作系统,但是我无法从主机操作系统访问我在容器内引入的服务。这是我的Vagrantfile:

  VAGRANTFILE_API_VERSION =2

Vagrant.configure(VAGRANTFILE_API_VERSION)do | config |
config.vm.network:forwarding_port,guest:8000,host:8000

config.vm.defineicecastdo | v |
v.vm.providerdockerdo | d |
d.image =moul / icecast
d.ports = [8000:8000]

d.env = {
#SOURCE_PASSWORD:'password ',
ADMIN_PASSWORD:'password',
#PASSWORD:'password',
#RELAY_PASSWORD:'password'
}
end
end
end

我的理解是,运行 vagrant up --provider = docker OS X上的将启动运行boot2docker的虚拟机,然后运行我的容器。运行 vagrant docker-logs 似乎确认我的容器已创建,服务已启动,但现在我无法为我的生活找出如何访问服务我的OS X主机。如果我正在使用标准的VirtualBox提供程序,我会期望 config.vm.network:forwarding_port 指令来处理转发,但是添加似乎没有什么区别。



我需要做些什么才能从我的OS X主机访问这项服务?



更新:作为参考,以下是该图像的Docker文件: https:// github。 com / moul / docker-icecast / blob / master / Dockerfile

解决方案

好的,我终于弄清楚了而事实证明,解决方案是根本不使用boot2docker。基于一些潜水,我通过Vagrant来源,阅读问题和重新观察Docker提供商介绍视频,结果是您需要使用代理VM托管您的容器而不是boot2docker。



要设置此项,我修改了我的Vagrantfile以包含 vagrant_vagrantfile 的配置选项:

  VAGRANTFILE_API_VERSION =2

Vagrant.configure(VAGRANTFILE_API_VERSION)do | config |
config.vm.defineicecastdo | v |
v.vm.providerdockerdo | d |
d.image =moul / icecast
d.ports = [8000:8000]

d.env = {
#SOURCE_PASSWORD:'password ',
ADMIN_PASSWORD:'password',
#PASSWORD:'password',
#RELAY_PASSWORD:'password'
}

d.vagrant_vagrantfile = ./Vagrantfile.proxy
end
end
end

然后,我添加了一个Vagrant将用来启动代理VM的附加文件(Vagrantfile.proxy):

  VAGRANTFILE_API_VERSION = 2

Vagrant.configure(VAGRANTFILE_API_VERSION)do | config |
config.vm.box =ubuntu / trusty64
config.vm.provisiondocker
config.vm.provisionshell,inline:
ps aux | grep'sshd:'| awk'{print $ 2}'| xargs kill

config.vm.network:forwarding_port,guest:8000,host:8000
end

使用Docker配置程序将自动为您的代理VM安装Docker。内联shell脚本强制Vagrant重新登录到盒子中,以便它可以在Docker安装后使用。最后,我在这个Vagrantfile中转发我需要的端口,而不是原始端口(同时仍然使用原始的端口配置选项)。



就像使用默认的boot2docker策略一样,Vagrant将非常聪明,可以重用现有的代理虚拟机实例,以利用它的任何图像。



希望这将有助于有人在路上。


I'm trying to utilize Vagrant 1.6's Docker provider and I seem to have run into a snag. I can successfully bring up the Docker container and guest OS, but then I can't access the service I've brought up within the container from the host OS. Here's my Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.network :forwarded_port, guest: 8000, host: 8000

  config.vm.define "icecast" do |v|
    v.vm.provider "docker" do |d|
      d.image = "moul/icecast"
      d.ports = ["8000:8000"]

      d.env = {
        # SOURCE_PASSWORD: 'password',
        ADMIN_PASSWORD: 'password',
        # PASSWORD: 'password',
        # RELAY_PASSWORD: 'password'
      }
    end
  end
end

My understanding is that running vagrant up --provider=docker on OS X will start a VM running boot2docker that will then run my container. Running vagrant docker-logs seems to confirm that my container is created and the service started, but now I can't for the life of me figure out how to access the service from my OS X host. If I was using a standard VirtualBox provider, I would expect the config.vm.network :forwarded_port directive to handle the forwarding, but adding that doesn't seem to make any difference.

What do I need to do to be able to access this service from my OS X host?

Update: For reference, here is the image's Dockerfile: https://github.com/moul/docker-icecast/blob/master/Dockerfile

解决方案

Ok, so I finally figured this out and it turns out the solution is to not use boot2docker at all. Based on some diving I did through the Vagrant source, reading issues, and rewatching the Docker provider introduction videos, it turns out that you need to use a proxy VM to host your containers instead of boot2docker.

To set this up, I modified my Vagrantfile to include a configuration option for vagrant_vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "icecast" do |v|
    v.vm.provider "docker" do |d|
      d.image = "moul/icecast"
      d.ports = ["8000:8000"]

      d.env = {
        # SOURCE_PASSWORD: 'password',
        ADMIN_PASSWORD: 'password',
        # PASSWORD: 'password',
        # RELAY_PASSWORD: 'password'
      }

      d.vagrant_vagrantfile = "./Vagrantfile.proxy"
    end
  end
end

Then I added an additional file (Vagrantfile.proxy) that Vagrant will use to spin up the proxy VM:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "docker"
  config.vm.provision "shell", inline:
    "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"

  config.vm.network :forwarded_port, guest: 8000, host: 8000
end

Using the Docker provisioner will automatically install Docker on the proxy VM for you. The inline shell script forces Vagrant to log back into the box so that it can utilize Docker after it's been installed. Finally, I forward the port I need in this Vagrantfile as opposed to the original (while still using the ports config option in the original).

Just like with the default boot2docker strategy, Vagrant will be smart enough to reuse existing instances of the proxy VM for any image that utilizes it.

Hopefully this will be helpful to someone down the road.

这篇关于使用Vagrant Docker提供程序从boot2docker转发端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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