Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a folder created inside vagrant?

Tags:

vagrant

ubuntu

I have a vagrant VM and I have shared a folder (my code repo) from the host (Ubuntu) to vagrant using config.vm.share_folder. I would like to do the opposite with a folder I have created inside the vagrant machine (a virtual environment) I would like to share it back to the host. How can I do that?

I have tried to add the following to the vagrant file: config.vm.share_folder "virtualenv", "/home/vagrant/devenv", "../virtualenv" which points respectively to the virtual environment on the vagrant machine and to an empty folder on the host. When I vagrant up and look inside the folder on the host I would like to see the content of the virtual env inside the vagrant machine but the folder stays empty. And when I ssh into the vagrant machine and look inside the virtual env folder it has become empty. Deactivating this setting restores the content of the folder on the vagrant machine.

like image 889
Bastian Avatar asked Oct 07 '13 18:10

Bastian


People also ask

Which is the default shared folder created on the guest machines by vagrant?

By default, Vagrant shares your project directory (the one containing the Vagrantfile) to the /vagrant directory in your guest machine.

Where is the vagrant folder?

Explore the synced folder Tip: When you vagrant ssh into your machine, you're in /home/vagrant , which is a different directory from the synced /vagrant directory. Believe it or not, the Vagrantfile that you see inside the virtual machine is actually the same Vagrantfile that is on your actual host machine.


1 Answers

shared folders VS synced folders

Shared folders has been renamed to synced folders from v1 to v2 (docs), under the bonnet it is still using vboxsf between host and guest (there is known performance issues if there are large numbers of files/directories).

NOTE: You need to understand it mounts host directory into the guest via vboxsf, NOT the other way around.

In your use case, you can

  1. use synced folder to map folders between host and guest (suppose they both are empty), then within the guest, copy (rsync is preferred) or move the project into the mapped folder.
  2. copy the contents from guest to host (using scp or rsync) folder A, and then use synced folder to map folder A on host into guest.

For example

# relative path to where Vagrantfile resides
config.vm.synced_folder "virtualenv", "/home/vagrant/devenv"

# absolute path
config.vm.synced_folder "/path/to/virtualenv", "/home/vagrant/devenv"
  1. maps the virtualenv directory in the project directory (where the Vagrantfile resides) to guest /home/vagrant/devenv.

  2. absolute path

More information that can help you understand how synced folders work => Vagrant shared and synced folders

like image 162
Terry Wang Avatar answered Oct 21 '22 02:10

Terry Wang