简体   繁体   English

流浪者和Docker提供者:一种为Linux主机强制代理VM的方法?

[英]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? TL&DR: 即使主机操作系统本身支持Docker,是否有一种方法可以强制Vagrant使用代理VM?

I'm using Vagrant with Docker provider. 我正在将Vagrant与Docker提供程序一起使用。 The Vagrant VM is the OS and Docker containers host my apps (web servers, DBs). Vagrant VM是OS,而Docker容器托管了我的应用程序(Web服务器,DB)。

Problem: 问题:

Linux containers do not run natively on non-Linux machines. Linux容器不能在非Linux机器上本地运行。 If your developers are on Mac or Windows, they can't run Docker containers natively. 如果您的开发人员是在Mac或Windows上,则他们不能本地运行Docker容器。 Vagrant detects these cases and automatically spins up a Linux virtual machine to run the Docker containers. Vagrant检测到这些情况,并自动启动Linux虚拟机以运行Docker容器。

[...] [...]

If Vagrant is being used with Docker on Linux, Vagrant won't automatically spin up a virtual machine and instead will run Docker natively 如果Vagrant与Linux上的Docker一起使用,则Vagrant不会自动启动虚拟机 ,而是将在本地运行Docker

Source: http://www.vagrantup.com/blog/feature-preview-vagrant-1-6-docker-dev-environments.html 资料来源: 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. Vagrant自动为不支持Docker的OS启动一个代理虚拟机非常好,因为它们具有相同的OS。 But for Linux hosts, we're stuck with native Docker installation, which cause few problems: 但是对于Linux主机,我们受困于本机Docker安装,这不会引起什么问题:

  • file/folder permission 文件/文件夹权限
  • different user for different Linux OS (Apache user is sometime "apache", sometime "www", depending of your Linux distro) 不同Linux操作系统的不同用户(Apache用户有时是“ apache”,有时是“ www”,具体取决于您的Linux发行版)

Here is my Vagrant files for references: 这是我的Vagrant文​​件供参考:

DockerHost.Vagrantfile 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 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

Just use d.force_host_vm = true option 只需使用d.force_host_vm = true选项

From Vagrant docs: 从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. force_host_vm(布尔值) -如果为true,则即使运行Vagrant的计算机支持Linux容器,也会启动主机VM。 This is useful to enforce a consistent environment to run Docker. 这对于强制使用一致的环境来运行Docker非常有用。 This value defaults to "true" on Mac and Windows hosts and defaults to "false" on Linux hosts. 在Mac和Windows主机上,此值默认为“ true”,在Linux主机上,默认值为“ false”。 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. 选择使用其他Docker提供程序或选择加入本机Docker构建的Mac / Windows用户可以将此值显式设置为false以禁用该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM