Jonathan Andersson
Jonathan Andersson

Reputation: 1432

Vagrant synced folders writes over hosts files when vagrant up

I'm using Vagrant to set up an ubuntu box on Virtualbox. In the vagrant file I've specified synced folders to sync my code with the guest:

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

The problem I have only appears when I the machine is destroyed and then rebuilt with vagrant up. In this case Vagrant syncs my folders on the guest to my folder on the host, instead of host to guest.

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

I don't like this behaviour at all, if I haven't checked in my code on the host to my version control I will loose my code.

Should the sync be bidirectional? Is there a way to configure it to only sync one way preferable host to guest. Could not find anything about this in vagrant basic sync

In comments below there where suggestions that it where my provisioning (Chef-Solo) that where the cause of the problem. To exclude the provisioning phase from this I did following:

Vagrant up --no-provision

The hosts content where synced to the guest.

rm -r /guest/webapp/ on the guest

Resulted in the hosts content where removed as well.

mkdir /guest/webapp/test on the guest

The created folder test where also created on the host.

I think this proves that the sync is bidirectional.

Upvotes: 0

Views: 1071

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53793

so vagrant Synced folders are really synced (probably why they're not just called shared folder) so once you sync your folder whatever action you make on one side will be synchronized/replicated automatically on the other side. (wether it comes from the host or the guest)

If you really want a one-way sharing, you can look at [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.

Upvotes: 1

Related Questions