Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 796
AndrewSwerlick Avatar asked Mar 12 '15 22:03

AndrewSwerlick


People also ask

Which interface should the network bridge to Vagrant?

Default Network Interface If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.

How do I make my Vagrant network private?

The easiest way to use a private network is to allow the IP to be assigned via DHCP. This will automatically assign an IP address from the reserved address space. The IP address can be determined by using vagrant ssh to SSH into the machine and using the appropriate command line tool to find the IP, such as ifconfig .

Can you run Vagrant in Docker?

Vagrant comes with support out of the box for using Docker as a provider. This allows for your development environments to be backed by Docker containers rather than virtual machines.

What is the difference between VirtualBox and Vagrant?

Introduction. VirtualBox is basically inception for your computer. You can use VirtualBox to run entire sandboxed operating systems within your own computer. Vagrant is software that is used to manage a development environment.


1 Answers

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 .

like image 180
Ruaidhrí Primrose Avatar answered Sep 22 '22 18:09

Ruaidhrí Primrose