luke7233
luke7233

Reputation: 11

Access Vagrant (Virtualbox) from Docker

My question is as follows. I am a webdeveloper and have several applications running in both Docker and Vagrant (Virtualbox). Normally it is just in one of these two. But now I have an application that is partly running in Docker and partly in Vagrant. Now I need to connect from the application in the Docker container to the MySQL server in Vagrant box.

On my host computer (MacOS) is the Virtual box network shown as:

vboxnet1: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500 ether 0a:00:27:00:00:01 inet 192.168.33.1 netmask 0xffffff00 broadcast 192.168.33.255

I want to bridge this connection to my Docker container so can for example ping from my Docker box to ip 192.168.33.1

I tried to add the network as bridged network (with the same ip) and make it like this available to the container. But then I get an error that the submet already is taken.

Also I tried to add the Vagrant box to the local network so it gets an ip from the router (see below). But my box does not get an ip (maybe this is because I am on large business network)

config.vm.network "public_network", bridge: "en0: Wi-Fi", ip: "10.128.11.188", :netmask => "255.255.255.0"
  # and also tried:
config.vm.network "public_network", bridge: "en0: Wi-Fi", type: "dhcp"

I found the topic below, which I tried. This wasnt working on MacOS. The adapter shows not up in adapter list, so cannot select it on Virtualbox.

Access vagrant VMs from inside docker container

Does anyone have an idea to accomplish this?

Upvotes: 0

Views: 119

Answers (1)

luke7233
luke7233

Reputation: 11

Okay, Guess I found already a way:

Forget about getting Vagrant and Docker on the same network. Just forward the specific ports on Vagrant:

  config.vm.network "forwarded_port", guest: 3306, host: 3307

And in Docker make sure you use the following setting:

   extra_hosts:
       - 'host.docker.internal:host-gateway'

Now use "host.docker.internal" as ip with port 3307 in Docker to connect to the MySQL server in the Vagrant VM box.

Upvotes: 1

Related Questions