如何使用vagrant 1.6和docker provider映射docker ports

时间:2014-05-07 23:39:52

标签: vagrant docker vagrantfile boot2docker

我目前正在尝试将我的docker容器端口从容器映射到主机(boot2docker)。最终目标是将这些端口映射到我的物理机器,但一次只能一步。

我的Vagrantfile目前看起来像:

Vagrant.configure("2") do |config|

    config.vm.define "app1" do |a|
        a.vm.provider "docker" do |d|
            d.build_dir = "dockers/app1"
            d.name = "app1"
            d.ports << "8080:8080"
            d.ports << "8443:8443"
            d.volumes << "/vagrant/data/app1:/var/app1"
        end
    end

    config.vm.define "app2" do |a|
        a.vm.provider "docker" do |d|
            d.build_dir = "dockers/app2"
            d.name = "app2"
            d.ports << "8081:8081"
            d.link("app1:app1")
        end
    end
end

当我运行vagrant up app1 --provider=docker时,容器正确旋转,但是当我执行docker ps时,我可以看到端口尚未映射。

0.0.0.0:2222->22/tcp, 8080/tcp, 8443/tcp

我正在使用VirtualBox,因此我使用GUI将我的物理机8080移植到主机(boot2docker)8080

1 个答案:

答案 0 :(得分:1)

您的配置应该适用于Linux,但如果您使用的是Virtualbox(我假设您使用的是Mac或Windows),那么您希望VM的Vagrantfile可以将其发送给您的主机。

Vagrant.configure("2") do |config|

  config.vm.box = "busybox"
  config.vm.provider "virtualbox" do |v|
    v.memory = 768
    v.cpus = 2
  end

  config.vm.network :forwarded_port,
    guest: 8080, host: 8080

end

让我们假装相对于您当前的Vagrantfile,在host-vm / Vagrantfile中。所以你当前的Vagrantfile应该是这样的:

Vagrant.configure("2") do |config|

    config.vm.define "app1" do |a|
        a.vm.provider "docker" do |d|
            d.vagrant_vagrantfile = "host-vm/Vagrantfile"
            d.build_dir = "dockers/app1"
            d.name = "app1"
            d.ports = ["8080:8080"]
            d.ports = ["8443:8443"]
            d.create_args = ["-v", "/vagrant/data/app1:/var/app1"]
        end
    end

    config.vm.define "app2" do |a|
        a.vm.provider "docker" do |d|
            d.vagrant_vagrantfile = "host-vm/Vagrantfile"
            d.build_dir = "dockers/app2"
            d.name = "app2"
            d.ports = ["8081:8081"]
            d.link("app1:app1")
        end
    end
end