0
votes

I am running Vagrant and Virtualbox on Mac OS X. I have created one vagrant box using Virtualbox provider (some_centos machine) and second box using docker provider. Below you will see docker definition.

config.vm.define "mysql" do |m|
    m.vm.hostname = "mysql-host"
    m.vm.synced_folder ".", "/vagrant", disabled: true
    m.ssh.insert_key = false
      m.ssh.username = 'docker'
      m.ssh.password = 'tcuser'
      m.vm.network "public_network", use_dhcp_assigned_default_route: true

m.vm.provider "docker" do |docker|

  # Specify the Docker image to use
  docker.image = "mysql/mysql-server"

  # Specify port mappings
  # If omitted, no ports are mapped!

  docker.create_args = ["-p", "3306:3306","-e","MYSQL_ROOT_PASSWORD=password", "-e", "MYSQL_DATABASE=database"]

  # Specify a friendly name for the Docker container
  docker.name = 'mysql-server-container'

  #We want to keep it running.
  docker.remains_running = true
end

end

Both machines do start up, but what I want to do is to access from "some_centos" host to that mysql docker machine by using mysql client to connect to it. Unfortunately I am failing. Please be aware that on Mac OS when you use vagrant docker provider it will create boot2docker iamge and then create docker. Not sure how to tackle the problem as e.g. I can ssh into "some_centos", but can't run mysql -h "docker-host" -uroot -p.

2

2 Answers

0
votes

Basically, you need to have double port forwarding. First, you forward container port to Vagrant, then vagrant port to your Mac port.

When running your docker container, use code like this:

docker container run -d --name nginx-test -p 8080:80 nginx

In your Vagrant file, add this line above end:

config.vm.network "forwarded_port", guest: 8080, host: 1200

This way, container's port 80 will be forwarded to vagrant's port 8080 and vagrant's port 8080 will be forward to host port 1200.

I hope this image will help:

docker-vagrant-port-forwarding

0
votes

I have found a workaround for this. So it looks like on MAC OS X you need also do port forwarding on that virtual machine, but manually. So you need to run following:

VBoxManage controlvm name_of_your_virtual_machine natpf1 "mysql,tcp,,3306,,3306""

Then I was able to do the mysql. So I will need to add this a shell provisioner to my Vagrantfile.