With Vagrant now installed, let’s have a look at getting started with Vagrant. In this article, we’ll have a look at several topics. First off, we’ll explore the concept of Synced Folders and how Vagrant sets up file sharing between the host and guest OS. Then we’ll move on to working with a few different commands in Vagrant. We will look at vagrant suspend, vagrant resume, vagrant status, vagrant halt, vagrant up, and a few others. Next up will be destroying virtual machines with Vagrant, and why you don’t need to get too excited about VM destruction. The Vagrantfile makes it easy to build another one if you like. Finally, we’ll talk about where to find help with getting started with Vagrant. Let’s check it out.
Synced Folders
A great feature of creating virtual machines with Vagrant is that of automatic file sharing between host and guest operating systems. This is better known as synced folders. The host operating system is the OS of the host machine. This is what the type 2 hypervisor runs on top of. These demonstrations are making use of VirtualBox, however many other providers are available with VMware being a popular choice. Operating systems that run in the hypervisor are referred to as Guests. Let’s check out a quick demonstration to prove that file sharing is already enabled.
To begin, we will create a new file on our host operating system named my_shared_file
via the simple touch
command. A quick ls
shows that the file is now created.
With the file now created on the host OS, we can use nano my_shared_file
to add some text to the file. Like the file says, we will be able to access this file on the guest OS too as Vagrant sets up synced folders right away. By default, the folder which contains the Vagrantfile on the host will be shared with the /vagrant folder on the guest.
From our host, we will now SSH into the guest via vagrant ssh
. Once logged into our guest OS, we change to the /vagrant directory via cd /vagrant
, and then list it’s contents with ls
. We see the file is there automatically! To prove it is the same file, we can view it’s contents with the cat
command and the text is in fact the same that we created on the host.
The great thing about shared files, or synced folders, is that it makes it easy to work on the guest with the editors and tools of your choice. All testing happens on the guest, which is a closer replication of a production environment.
Vagrant Suspend and Vagrant Resume
With Vagrant, you can create multiple environments to test out various configurations of components. If you’d like to switch between projects and retain the state of the current VM, you can make use of the vagrant suspend
command to do so. First, we can be sure that our virtual machine guest is running both from Vagrant and VirtualBox. We’ll use vagrant status
and vboxmanage list runningvms
to see this.
Let’s now suspend the virtual machine with vagrant suspend
When we are ready to bring this VM back online, we can make use of vagrant resume
like so.
The key takeaway when using vagrant suspend and vagrant resume, is that this approach saves the state of the virtual machine.
Vagrant Halt and Vagrant Up
If you are not concerned with saving the current state of the current running VM, you can make use of vagrant halt
instead of vagrant suspend
. One reason why you might choose vagrant halt over vagrant suspend is that with vagrant suspend, it takes extra space to store the contents of the guest VM’s memory. vagrant halt
is akin to powering down a computer, where as vagrant suspend
is more along the lines of putting a computer into sleep mode. Let’s test out vagrant halt
now.
In order to bring a halted VM back online, you would need to make use of vagrant up
.
The takeaway with vagrant halt and vagrant up is that this approach conserves resources on the host, but the guest will take longer to boot after a halt. So now we know the difference between using each approach.
Destroying VMs in Vagrant
In order to recover all resources and disk space on the host, we can destroy a VM instead of just suspending or halting it. It might feel like you’re losing a precious development environment by doing this, but really you’re not losing anything. When using vagrant, all you need is that valid Vagrantfile, and you will be able to recreate the environment again. In fact, you can just as well put the Vagrantfile right into version control and other developers can recreate the same exact environment very quickly on the machine they have Vagrant installed on. This is the power Vagrant offers. Before we destroy our VM, let’s once again confirm it is running.
Note the unique identifier of the guest virtual machine “ubuntu_default_1469830649698_22953” {4c253c3e-de65-4285-82b5-3a2a84d9f69d}. Let’s see if that stays the same, or if we get a new one after a vagrant destroy
.
After destroying the VM, we can see that there are in fact no running virtual machines when we check the status from both Vagrant and VirtualBox. Now, consider that this virtual machine is entirely deleted and gone. What we still have, however, is the Vagrantfile. It is still in that Ubuntu directory we had created on the host machine. With that file, it only takes us a few minutes to spin up a whole new virtual machine. Let’s see.
We can view the updated status.
Check that out! We have an entirely brand new virtual machine running now. Notice the unique identifier on this new VM, it is “ubuntu_default_1470073198181_65287” {580d88af-93b4-4c90-81b8-3fa1bff04b84}. This is completely different than the original VM we were working on which was “ubuntu_default_1469830649698_22953” {4c253c3e-de65-4285-82b5-3a2a84d9f69d}. So the takeaway from this is that vagrant suspend
and vagrant halt
are ways of stopping the VM, which can then be resumed. If you run a vagrant destroy
however, you are entirely wiping out the VM and associated files with that VM. This is like manually deleting a virtual machine in the VMware or VirtualBox GUI. With our Vagrantfile however, it is quick and painless to spin up a new virtual machine right away. During your time working with Vagrant, you will find which approach works best for you with regard to stopping, resuming, destroying, and creating virtual machines.
Getting Help With Vagrant
There are a few ways to get more help with Vagrant when you’re starting out. Check out these great resources.
- Vagrant Getting Started Guide
- Vagrant’s creator on Twitter
- The official Vagrant blog
- The Vagrant Source Code
- Vagrant on Google Groups
- The
vagrant help
command, reference shown below.
vagrant -v, –version | Print the version and exit. |
vagrant -h, –help | Print this help. |
vagrant box | manages boxes: installation, removal, etc. |
vagrant destroy | stops and deletes all traces of the vagrant machine |
vagrant global-status | outputs status Vagrant environments for this user |
vagrant halt | stops the vagrant machine |
vagrant help | shows the help for a subcommand |
vagrant init | initializes a new Vagrant environment by creating a Vagrantfile |
vagrant login | log in to HashiCorp’s Atlas |
vagrant package | packages a running vagrant environment into a box |
vagrant plugin | manages plugins: install, uninstall, update, etc. |
vagrant port | displays information about guest port mappings |
vagrant powershell | connects to machine via powershell remoting |
vagrant provision | provisions the vagrant machine |
vagrant push | deploys code in this environment to a configured destination |
vagrant rdp | connects to machine via RDP |
vagrant reload | restarts vagrant machine, loads new Vagrantfile configuration |
vagrant resume | resume a suspended vagrant machine |
vagrant snapshot | manages snapshots: saving, restoring, etc. |
vagrant ssh | connects to machine via SSH |
vagrant ssh-config | outputs OpenSSH valid configuration to connect to the machine |
vagrant status | outputs status of the vagrant machine |
vagrant suspend | suspends the machine |
vagrant up | starts and provisions the vagrant environment |
vagrant version | prints current and latest Vagrant version |
For help on any individual command run vagrant COMMAND -h
Additional subcommands are available, but are either more advanced |
Benefits of using Vagrant
There are almost too many benefits to list for using Vagrant, but we will try.
- Bring up an entire virtualized environement with one command: vagrant up
- Developers are able to try different operating systems and software easily on the same machine.
- New virtual machines only take a few minutes to boot.
- Virtual Machines can be used in the same configuration by all team members.
- All team members have the same exact software to test on.
- Every team member can create, destroy, and re-create virtual environments within a few short minutes.
- Access to existing physical resources, such as network ports, are restricted.
- Vagrant makes it easy to update server-side software on developer machines.
- One to One mapping across development, testing, and production environments is now possible, and fairly easy to accomplish.
- Sysadmins can set up very specific rules and resources to ensure security for sandboxed virtual machines.
- Each project can use wildly different server side software packages as needed. Project 1 can use a LEMP stack while project two makes use of a MEAN stack.
- Developers don’t even need a network connection to get work accomplished.
- Vagrant is Free and Open Source to the entire community!
- It has fantastic documentation.
- Vagrant can be extended even further with plugins if you find it necessary.
Getting Started With Vagrant Summary
This was a nice introduction to getting started with Vagrant, the fantastic DevOps software that makes configuring virtual environments easy. We had a look at the basics of getting up and running with Vagrant along with some good resources for learning more about this great tool.