Access other computer network only on vagrant/virtual box host from within docker container


Andrew Swick

I have a lab environment that is setup using vagrant. It consists of 3 computers, 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.

Set up your environment with a host-only private network. This is 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 found that when I go inside the container on app1 or app2, I can't access the database server. I think the problem 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 every computer on the network. However, in the container, because this interface is not configured, the container treats all 127.0.0.x requests as requests to localhost and sends them back to itself.

Is there any alternative configuration I can set on vagrant side or docker side to mitigate this issue? In short, I want a container on my app server to communicate with the db server in a do nothing environment. Note that the database is installed directly on the db-host, not running inside a docker container. Also, this is to mimic a production environment where vagrant won't be used, so I would expect all docker changes to work in a more normal network situation as well.

Primrose Roy

By default, the VirtualBox private network is host-only, so one VM cannot see the other. You can change it to use the VirtualBox internal network by using the virtualbox__intnet setting, so you needhost.vm.network "private_network", ip: ip, virtualbox__intnet: true

More info here http://docs.vagrantup.com/v2/virtualbox/networking.html

Note that this is specific to VirtualBox. If this also needs to be used with other providers, I think you will need to use the public network to get the necessary bridging and consider all the relevant issues related to ensuring security. See http://docs.vagrantup.com/v2/networking/public_network.html .

Related


Unable to access port on MacOSX host from within Docker container

Xiphias These are the steps I took: Before work: First: docker-machine create -d virtualbox default Then, I created a container with the following Dockerfile: FROM centos:latest Nothing - just a copy of CentOS. I build the container: docker build -t mycontain

Ping Docker Container from another computer in the network

Kartik I created a docker container and tried to ping www.google.com in the bash of that container and it works. I also tried pinging the container from the host - it worked just fine. However, when I try to ping the container from an external system in the ne

How to ping Docker-Container within host network?

nail I've been using Docker containers for a while now, but don't know how to ping a docker container that is part of my host's network. So so far I created my container, specifying the name and network flags as described in many tutorials : https://www.digita

Access host database from Docker container

Ryan Anderson: If I have a mysql database running on some host, and that host is also running a docker container: how can I access the mysql database from a docker container running on the host? For example, is there a way to publish the host port to the conta

How to access host port from Docker container

Tri Nguyen: I have a docker container running jenkins. As part of the build process, I need to access a web server running locally on the host machine. Can the host web server (which can be configured to run on a port) be exposed to the jenkins container? Edit

Access host database from Docker container

Ryan Anderson If I have a mysql database running on some host, and that host is also running a docker container: How can I access the mysql database from a docker container running on the host? For example, is there a way to publish the host port to the contai

Docker: Access a service on the host from a container

SmxCde I need to access a service running on the host from a docker container using a docker-compose settings file. Also, at this point, I don't know if I'll be able to change the hostname used by the application in the container, so the service must be availa