Forward Ports from boot2docker using the Vagrant Docker provider

后端 未结 2 866
粉色の甜心
粉色の甜心 2020-12-31 11:50

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 ac

2条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 12:14

    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.

提交回复
热议问题