当 vagrant up 时,Vagrant 同步文件夹会覆盖主机文件

Vagrant synced folders writes over hosts files when vagrant up

我正在使用 Vagrant 在 Virtualbox 上设置一个 ubuntu 盒子。 在 vagrant 文件中,我指定了同步文件夹以将我的代码与来宾同步:

config.vm.synced_folder "/host/target/webapp", "/guest/webapp"

我遇到的问题只有当我的机器被破坏然后用 vagrant up 重建时才会出现。在这种情况下,Vagrant 将来宾上的文件夹同步到主机上的文件夹,而不是主机到来宾。

"/guest/webapp" ---> "/host/target/webapp"

我根本不喜欢这种行为,如果我没有将主机上的代码签入我的版本控制,我将丢失我的代码。

同步应该是双向的吗?有没有一种方法可以将其配置为仅以一种方式将首选主机同步到来宾。在 vagrant basic sync

中找不到任何相关信息

在下面的评论中,建议我的配置 (Chef-Solo) 是问题的原因所在。 为了从中排除配置阶段,我做了以下操作:

Vagrant up --no-provision

同步到访客的主机内容。

rm -r /guest/webapp/ on the guest

导致主机内容也被删除。

mkdir /guest/webapp/test on the guest

创建的文件夹测试也在主机上创建。

我认为这证明同步是双向的。

所以 vagrant Synced folders 是真正同步的(可能是为什么他们不只是被称为共享文件夹)所以一旦你同步你的文件夹,你在一侧所做的任何操作都会在另一侧自动 synchronized/replicated对方。 (无论是主人还是客人)

如果真的想要单向分享,可以看看[rsync](https://www.vagrantup.com/docs/synced-folders/rsync.html)

Vagrant can use rsync as a mechanism to sync a folder to the guest machine. This synced folder type is useful primarily in situations where other synced folder mechanisms are not available, such as when NFS or VirtualBox shared folders are not available in the guest machine.

The rsync synced folder does a one-time one-way sync from the machine running to the machine being started by Vagrant.