Vagrant 错误 - 同名的 VirtualBox 机器已经存在

Vagrant error - A VirtualBox machine with the name already exists

我想使用 ubuntu/xenial64 框为两个单独的项目创建两个单独的 VM。我在两个单独的项目目录中定义了 Vagrantfile,并在每个目录中添加了 config.vm.box = "ubuntu/xenial64" 行。

第一个盒子启动成功。但是当我为第二个项目做 vagrant up 时,我得到了错误

A VirtualBox machine with the name 'ubuntu-xenial-16.04-cloudimg' already exists.

在 Vagrant 的文档中明确写道

Boxes are globally stored for the current user. Each project uses a box as an initial image to clone from, and never modifies the actual base image. This means that if you have two projects both using the hashicorp/precise64 box we just added, adding files in one guest machine will have no effect on the other machine.

为什么会出现这个错误?

我已经检查过 other similar questions,但我不明白他们删除似乎具有相同名称的现有 VM 的解决方案。根据上面的 Vagrant 文档引用,这不是必需的。我错过了什么吗?

您不需要删除其他 VM,实际上您肯定可以从同一个盒子中拥有多个 VM。

您的错误可能与在 VirtualBox 中创建的 VM 的 VirtualBox 名称有关,如果您重写了 属性 来设置此名称并且在您的 2 个项目中设置了相同的名称,那么将会出现冲突,请参阅此 answer 以查看定义 VM 名称的不同方法

因此,要么让 vagrant 定义 VM 的名称,要么确保您在不同的项目中具有唯一的 VM 名称,这样 运行 就好了

更新 我选中了这个特定的框,它包含以下 Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.base_mac = "02101FC67BA9"
  config.ssh.username = "ubuntu"
  config.ssh.password = "c1580f876b655137c6c35b69"
  config.vm.synced_folder '.', '/vagrant', disabled: true

  config.vm.provider "virtualbox" do |vb|
     vb.name = "ubuntu-xenial-16.04-cloudimg"
     vb.customize [ "modifyvm", :id, "--uart1", "0x3F8", "4" ]
     vb.customize [ "modifyvm", :id, "--uartmode1", "file", File.join(Dir.pwd, "%s-console.log" % vb.name) ]
  end
end

所以确保在你的 Vagrantfile 中覆盖这个 属性

  config.vm.provider "virtualbox" do |vb|
     vb.name = "your specific project name"

并将 vb.name 更改为对您的每个项目都是唯一的。

我发现编辑原始框的 Vagrantfile(位于 ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-xenial64/<VERSTION>/virtualbox/Vagrant‌​file)比每次都考虑唯一的 VM 名称更简单。

Vagrantfile(盒子!)的工作配置:

include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

Vagrant.configure("2") do |config|
  config.vm.base_mac = "0223C61ABA59"
  config.ssh.username = "ubuntu"
  config.ssh.password = "86f7d0e04910475d8789aa8f"
  config.vm.synced_folder '.', '/vagrant', disabled: true

  config.vm.provider "virtualbox" do |vb|
     vb.customize [ "modifyvm", :id, "--uart1", "0x3F8", "4" ]
  end
end