Introduction to Vagrant
As a Linux Low-Level developer, I always find myself in need for multiple Linux VMs to fiddle with.
Whether because I need to check something on multiple distributions, or simply because my newest hack to probe something in the Kernel will probably burn the VM and anything else around it.
After using native VirtualBox for many years, creating, managing and deploying machines, reverting to snapshots or whatever I decided to try out Vagrant, which seems quite mature and widely used nowadays. So what is Vagrant? By their own words:
“Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.”
In practical terms, their primary goal seems to be improving the workflow of using VMs, so that it is more convenient, faster and easily shareable. Vagrant integrates with many VM providers and exports a simpler API for working with the actual VMs, including setting them up, cloning them and of course using them.
After a week or so of using it, I realized Vagrant really did speed things up during my day-to-day development process, and better yet, helped me help other teams in my development group integrate with my product much better.
Let me introduce the basic flow for using Vagrant:
First, we want to choose ourselves a Box (The naming convention for a Vagrant image). Luckily, we have a vast selection of Boxes eagerly waiting for us to abuse them at their website.
Now, assuming we chose
ubuntu/xenial64, let’s go and create our local environment:
Our last operation created a
Vagrantfile that may be used for provisioning and configuration of the guest machine. We’ll talk about this a bit later down the road.
For now, let’s create the guest machine (If the machine already exists, this command starts it):
And finally, let’s connect to it using ssh:
Now that we’re inside the guest machine, we can do anything we want, just like a normal VM.
To shut down the machine, we can use the following command:
NOTE: As you can see, all the vagrant commands should be executed from within the local environment directory (In our example
Sharing a directory between the Host and the Guest machines:
As we mentioned before, we can use the
Vagrantfile for configuration and provisioning of the guest machine. Right now, we’ll be using it to share a single directory between the Host and the Guest machines.
First, let’s shut down the guest machine. Then, open
Vagrantfile using your favorite text editor and find the following line:
# config.vm.synced_folder "../data", "/vagrant_data"
To enable syncing, we should:
- Remove the comment mark (‘#’) from the beginning
../datato be the directory that we want to sync on the Host machine
/vagrant_datato be the directory that we want to sync on the Guest machine
Power the guest machine back up and check that everything works.
Hope you enjoyed and found this helpful! See you around next time :)