Skip to main content

Syncing Files with Vagrant

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:15:00

Difficulty

Beginner

When using Vagrant, being able to sync applications, infrastructure, and other important files between your workstation and the resulting environment is paramount. It ensures that any changes we make to our working environment will persist after a vagrant destroy — after all, the Vagrant environment is ephemeral. In this hands-on lab, we will specifically explore syncing our host and guest directories using the rsync implementation by updating our Vagrantfile.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Syncing Files with Vagrant

Introduction

In this hands-on lab, we will specifically explore syncing our host and guest directories using the rsync implementation by updating our Vagrantfile.

Solution

Begin by logging in to the lab server using the credentials provided on the hands-on lab page:

ssh cloud_user@PUBLIC_IP_ADDRESS

Update the Vagrantfile

  1. View our wanderer directory:

    ls
  2. Move into the directory with:

    cd wanderer/
  3. Open the Vagrantfile with a text editor like so:

    vim Vagrantfile
  4. Use the config.vm.synced_folder option to set up file syncing based on the parameters from the instructions:

    Vagrant.configure("2") do |config|
     config.vm.box = "fgrehm/trusty64-lxc"
     config.vm.define "app" do |app|
       app.vm.provider :lxc do |lxc|
         lxc.container_name = 'app'
       end
     end
     config.vm.synced_folder "/home/cloud_user/wanderer/app", "/opt/wanderer", type: "rsync",
       id: "wanderer-app", rsync__auto: true
    end
  5. Save and exit.

Test the Vagrant Environment and Check the Sync

  1. Deploy the Vagrant environment:

    vagrant up
  2. We can SSH into the guest container to see that the sync worked:

    vagrant ssh
    ls /opt/wanderer/
    exit

Test the Auto-Syncing Process

  1. Since we are using rsync, auto-synced must be turned on:

    vagrant rsync-auto &
  2. Take note of the process ID output.

  3. Update the application; here changing Do This! to To-Do:

    vim app/views/index.ejs
    
      To-Do
  4. Save and exit; notice the output from Vagrant.

  5. Verify the change with:

    vagrant ssh
  6. Kill the auto-sync process:

    pkill PID_OUTPUT
  7. Stop the container:

    vagrant halt

Conclusion

Congratulations - you've completed this hands-on lab!