Connect to MySQL in a docker container (Vagrant on Windows/VirtualBox)

I am trying to create a virtualised dev environment on Windows using Vagrant and Docker (as are a lot of people). The problem I have is that I cannot connect (or I dont understand how to) from MySQL Workbench running on my Windows laptop to my MySQL DB in a Docker container in Boot2Docker. This is how I visualise the connection:

MySQL Work bench -> 3306 -> Boot2Docker -> 3306 -> Docker -> MySql

  • Ruby Remote SDK configuration with Intellij broken - workarround?
  • openssh-server doesn't start in Docker container
  • Installing libLAS on docker python - No such file or directory
  • How to use GC in Docker Registry without accessing container
  • How to mount a directory in docker container to host
  • CI: Should I run tests directly or through Docker container?
  • However I cannot connect to the DB from MySQLWorkbench. I have tried connection to the Boot2Docker host 10.0.2.15 on 3306 and tcp over ssh using the private key of the Boot2Docker box “.vagrant\machines\dockerhost\virtualbox\id”

    What am I doing wrong/what have I misunderstood.

    My Vagrantfile:
    ENV[‘VAGRANT_DEFAULT_PROVIDER’] = ‘docker’

    DOCKER_HOST_NAME = "dockerhost"
    DOCKER_HOST_VAGRANTFILE = "./host/Vagrantfile"
    
    Vagrant.configure("2") do |config|
    
      config.vm.network "forwarded_port", guest: 3306, host: 3306
    
      config.vm.define "mysql" do |v|
        v.vm.provider "docker" do |d|
          d.image = "mysql"
          d.env = {
            :MYSQL_ROOT_PASSWORD => "root",
            :MYSQL_DATABASE     => "dockertest",
            :MYSQL_USER         => "dockertest",
            :MYSQL_PASSWORD     => "docker"
          }
          d.ports =["3306:3306"]
          d.remains_running = "true"
          d.vagrant_machine = "#{DOCKER_HOST_NAME}"
          d.vagrant_vagrantfile = "#{DOCKER_HOST_VAGRANTFILE}"
        end
      end
    end
    

    My hosts/Vagrantfile (describing my Boot2docker host) is:

    FORWARD_DOCKER_PORTS='true'
    Vagrant.configure(2) do |config|
    
    
      config.vm.provision "docker"
    
      # The following line terminates all ssh connections. Therefore
      # Vagrant will be forced to reconnect.
      # That's a workaround to have the docker command in the PATH
    
      #Clear any existing ssh connections
      ####NOTE: ps aux seems to give variable results depending on run -> process number can be ####first #or second causing provision to fail!!!
      config.vm.provision "clear-ssh", type: "shell", inline:
            "ps aux | grep 'sshd:' | awk '{print $1}'  | xargs kill"
    #        "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
    
    
      config.vm.define "dockerhost"
      config.vm.box = "dduportal/boot2docker"
      config.vm.network "forwarded_port",guest: 8080, host: 8080
    
    
      config.vm.provider "virtualbox" do |vb|
              vb.name = "dockerhost"
    
      end
    
    end
    

  • nginx reverse proxy docker - filter by location
  • Kitematic shows all docker images wrongly
  • Docker clone private repo - Permission denied
  • Dockerfile build yielding lstat error on ADD
  • Java webapp to Tomcat 8 running in Docker container at localhost
  • How can I make docker machine to get images from host before pulling any image for itself?
  • One Solution collect form web for “Connect to MySQL in a docker container (Vagrant on Windows/VirtualBox)”

    Solved. As I suspected the ports were not getting forwarded between the host machine and the docker host. The solution is to move the port forwarding config line:

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

    into the docker host Vagrant file. MySQL Workbench on the Windows host can then connect on localhost:3306.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.