Arenzel
Arenzel

Reputation: 1156

Vagrant and Docker provider: a way to force proxy VM for Linux hosts?

TL&DR: Is there a way to force a proxy VM to be used by Vagrant, even if the host OS supports Docker natively?

I'm using Vagrant with Docker provider. The Vagrant VM is the OS and Docker containers host my apps (web servers, DBs).

Problem:

Linux containers do not run natively on non-Linux machines. If your developers are on Mac or Windows, they can't run Docker containers natively. Vagrant detects these cases and automatically spins up a Linux virtual machine to run the Docker containers.

[...]

If Vagrant is being used with Docker on Linux, Vagrant won't automatically spin up a virtual machine and instead will run Docker natively

Source: http://www.vagrantup.com/blog/feature-preview-vagrant-1-6-docker-dev-environments.html

It's great that Vagrant automatically spin up a proxy VM for OS that doesn't support natively Docker, because they so have the same OS to work with. But for Linux hosts, we're stuck with native Docker installation, which cause few problems:


Here is my Vagrant files for references:

DockerHost.Vagrantfile

Vagrant.configure("2") do |config|

  config.vm.provision "docker"
  config.vm.box = "ubuntu/trusty64"
  config.vm.define "dockerhost"

  config.vm.network :forwarded_port, guest: 80, host: 8080
  config.vm.synced_folder "/sites", "/sites" [...]

  config.vm.provider :virtualbox do |vb|
    vb.name = "Vagrant-Dockerhost"
    vb.memory = 1024 # => Required by MySQL Server
  end

end

Vagrantfile

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'
DOCKER_HOST_NAME = "dockerhost"
DOCKER_HOST_VAGRANTFILE = "DockerHost.Vagrantfile"

Vagrant.configure("2") do |config|

  config.vm.define "mysql-server" do |v|
    v.vm.provider "docker" do |d|
      d.image = "mysql"
      d.name = "mysql-server"
      d.env = {
        MYSQL_ROOT_PASSWORD: "rootpasswd",
        MYSQL_USER: "mysqluser",
        MYSQL_PASSWORD: "userpasswd",
        MYSQL_DATABASE: "dev"
      }
      d.volumes = ["/mysql:/var/lib/mysql"]
      d.cmd = ["/entrypoint.sh", "mysqld"]
      d.remains_running = true
      d.vagrant_machine = "#{DOCKER_HOST_NAME}"
      d.vagrant_vagrantfile = "#{DOCKER_HOST_VAGRANTFILE}"
    end
  end

  config.vm.define "apache-server" do |v|
    v.vm.provider "docker" do |d|
      d.image = "lacavalerie/apache-server"
      d.ports = ["80:80"]
      d.name = "apache-server"
      d.link("mysql-server:db")
      d.volumes = [...]
      d.cmd = ["/scripts/setup.rb"]
      d.remains_running = true
      d.vagrant_machine = "#{DOCKER_HOST_NAME}"
      d.vagrant_vagrantfile = "#{DOCKER_HOST_VAGRANTFILE}"
    end
  end
end

Upvotes: 2

Views: 579

Answers (1)

SharpEdge
SharpEdge

Reputation: 1762

Just use d.force_host_vm = true option

From Vagrant docs:

force_host_vm (boolean) - If true, then a host VM will be spun up even if the computer running Vagrant supports Linux containers. This is useful to enforce a consistent environment to run Docker. This value defaults to "true" on Mac and Windows hosts and defaults to "false" on Linux hosts. Mac/Windows users who choose to use a different Docker provider or opt-in to the native Docker builds can explicitly set this value to false to disable the behavior.

Upvotes: 0

Related Questions