How to enable two-way folder sync in Vagrant with

2019-03-25 20:33发布

问题:

I haven't used Vagrant on Linux for a while. When I started using the new version (Vagrant 1.8), I was faced with a problem: files created inside the guest VM didn't appear on the host machine's synced folder.

How do I force Vagrant to sync files from the guest OS to the host OS?

回答1:

According to documentation, when type option config.vm.synced_folder parameter in Vagrantfile is not specified, Vagrant tries to choose best available option:

type (string) - The type of synced folder. If this is not specified, Vagrant will automatically choose the best synced folder option for your environment. Otherwise, you can specify a specific type such as "nfs".

Starting with version 1.5 Vagrant introduced new "rsync synced folders" feature.

So in my case type rsync was automatically chosen, which is one-way sync: from host to guest.

To make folders sync in two-way I added explicit definition in my Vagrantfile:

config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

Of course, this will work only for VirtualBox.

Two way sync is useful for workflows, where apllication on guest machine creating files, for example, database migration files in modern web frameworks.

Note: virtualbox synced folders have known performance issues when your project has large amount of files.



回答2:

Thanks for this it was exactly what I have looking for the past several days !!!!! I had this problem with Laravel 5, homestead and vagrant. Many answers out there but none that worked for me. I also had rsync in my folders section of my yaml file that I also had to remove. After these two changes file sharing was finally sync for me between the host and client.

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

Here is where I made the change to my Vagranfile.