Search code examples
ubuntuvagrant

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.


Solution

  • 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