简体   繁体   中英

Accessing other machines on a vagrant/virtual box host only network from within a docker container

I have a lab environment I have setup using vagrant. It consists of 3 machines, two application servers with docker 1.4.1 installed and one database server with postgres 9.3 installed. All machines are running Centos 6.6.

The environment is setup using a host-only private network. Here's the vagrant file

Vagrant.configure('2') do |config|
  config.vm.box = "centos-6.0-updated"

  {
    'db'    => '10.17.33.10',
    'app1'   => '10.17.33.11',
    'app2' => '10.17.33.12',
  }.each do |short_name, ip|
    config.vm.define short_name do |host|
      host.vm.network 'private_network', ip: ip
      host.vm.hostname = "#{short_name}.my.dev"
    end
  end
end

I'm finding that when I'm inside of a container on app1 or app2 I cannot access the db server. I believe the issue is that vagrant/virtualbox's host only private network uses addresses in the 127.0.0.x range. On the host, vagrant configures the loopback interface to handle sending requests to each machine on the network. However in the container, because this interface is not configured the container treats all 127.0.0.x requests as requests for localhost and just sends them back to itself.

Is there any alternative configuration I can setup on the vagrant side, or the docker side that will alleviate this issue? In short, I want to have a vagrant environment where containers on my app server can talk to the db server. Note the db is installed directly on the db-host, not running inside a docker container. Also, this is meant to mimic a production environment that will not use vagrant, so I'd want any docker changes to work in a more normal networking scenario as well.

By default VirtualBox private networks are host-only, so one VM can't see another. You can change this to instead use a VirtualBox internal network by using the virtualbox__intnet setting, so you'd need a line like host.vm.network "private_network", ip: ip, virtualbox__intnet: true

There's a bit more info on this here http://docs.vagrantup.com/v2/virtualbox/networking.html

Note that this is VirtualBox specific. If this needs to also work with a different provider I think you're going to need to use a public network in order to get the necessary bridging and take into account all the related issues with securing that. See http://docs.vagrantup.com/v2/networking/public_network.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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