Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant: How to sync folder from guest back to host?

Tags:

Why, because it is difficult to work and edit code via legacy editors in the guest. The whole purpose of vagrant is to be more easily for the developer, right:)?

As such I please someone to guide me in this situation:

  • Need to work on a project. It is git repo. Requires mysql, php, etc, etc.
  • VM works great(provided)
  • But editing files in this repo not so easy via ssh and "legacy" editors. Want to use favorite IDE.

On the VM repo is in /home/vagrant/src. I want it to be visible/editable in ../src in the host.

I read the docs and putted this in the Vagranfile:

config.vm.synced_folder '../src', '/home/vagrant/src'

This "works" except that it overwrites all contents from /home/vagrant/src with those from ../src which is empty.

Any workaround? I considered the possibility to clone the repo via git(publicly available via github) and sync the folder to the VM but this does not feel right and lose production configs too.

like image 731
Kkart Avatar asked Dec 09 '14 17:12

Kkart


People also ask

How do you sync vagrant?

Vagrant automatically syncs files to and from the guest machine. This way you can edit files locally and run them in your virtual development environment. By default, Vagrant shares your project directory (the one containing the Vagrantfile) to the /vagrant directory in your guest machine.

Where is the Vagrantfile located?

Vagrantfile in your Vagrant home directory (defaults to ~/. vagrant. d ). This lets you specify some defaults for your system user.

How do I add files to vagrant?

Command: vagrant upload source [destination] [name|id] This command uploads files and directories from the host to the guest machine.


2 Answers

Seems like you are looking for two way sync. If my understanding is correct this answer will help you. Please note that I have not yet tested these. Seems like the key is to use virtualbox sync type.

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".

config.vm.synced_folder '../src', '/home/vagrant/src', type: "virtualbox" 

Then again according to this doc,

If you are using the Vagrant VirtualBox provider, then VirtualBox shared folders are the default synced folder type. These synced folders use the VirtualBox shared folder system to sync file changes from the guest to the host and vice versa.

Therefore I'm not sure why your default type is not virtualbox. Anyway this is worth a shot I guess. Further going into docs there seems to be a bug in virtual box type explained in docs. Please follow the steps here if you are going to use virtual box sync type.

like image 141
Rajind Ruparathna Avatar answered Oct 19 '22 01:10

Rajind Ruparathna


My Vagrantfile looks like this:

Vagrant.configure(2) do |config|   config.vm.box = "bento/ubuntu-16.04"   config.vm.network "forwarded_port", guest: 80, host: 8080   config.vm.network :forwarded_port, guest: 22, host: 10170, id: "ssh"   config.vm.provision :shell, path: "VagrantProvision.sh"   config.vm.synced_folder "./", "/var/beeGame" end 

As you can see here I have the same statement like yours! Here my repo (you can try it like example), and it works for me! I use PhpStorm to open this repo and edit files and all updates automatically and sync from my host machine (my laptop) to guest machine (virtual).

In your case, you're doing all right, and vagrant has to overwrites all contents from /home/vagrant/src with those from ../src. It is right behavior!!! You need put your code into src dir on your host machine (your laptop) not on your guest machine (virtual)!

like image 21
cn007b Avatar answered Oct 19 '22 00:10

cn007b