Bastian
Bastian

Reputation: 5865

How to share a folder created inside vagrant?

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.

Upvotes: 17

Views: 22932

Answers (3)

Krucamper
Krucamper

Reputation: 371

You can create a shared folder in Vagrant with:

config.vm.synced_folder "data", "/var/www", :mount_options => ["dmode=777","fmode=666"]

Share Mac into Ubuntu path

config.vm.synced_folder "/Users/Myname/www-vagrant", "/var/www/vagrant",

owner: "www-data", group: "www-data"

Upvotes: 0

Terry Wang
Terry Wang

Reputation: 13920

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

Upvotes: 15

rakhim
rakhim

Reputation: 164

In both shared folders and synced folders (see http://docs.vagrantup.com/v2/synced-folders/ ) edits are going both ways. The host folder and Vagrant folder are always in sync. In other words, it is shared both ways, so you won't need to do anything.

Upvotes: 0

Related Questions