Vagrant makes it trivial to provision VMs on your desktop. With it, you can quickly setup, provision and teardown VMs. You can use this for running non-native applications, use complex application stacks without contaminating your machine, test Ansible playbooks, Chef recipes and Docker images.
We’ll run through the basic setup for installing required applications on your desktop and provisioning your first server.
Vagrant manages virtual machine providers like VirtualBox, VMWare, KVM etx. You need a provider to use Vagrant. VirtualBox is a good choice because it is pretty stable and, well, free.
When complete, there will be a new VirtualBox.app in /Applications. You can start it up, play with it, but we won’t really ever need to use VirtualBox in this way - Vagrant will manage our VMs for us.
If we already have VirtualBox, and it can manage VMs, why use Vagrant at all? Most importantly, it makes your desktop look very much like the cloud; you will likely use a very similar setup when deploying to the cloud. Also, everyone on your team can use the same Vagrant configuration so you can avoid the “It works on my computer” kind of problems. Also, also, if your development environment is virtualized, it is very easy to recreate during continuous integration and deployment. Pretty cool stuff when you wrap your head around it.
BTW: If the last post introduced Homebrew, is it paying for itself yet? Mac already made installs easy but this is amazing.
You will likely have many VMs, so how do you organize them so they will make sense when you walk back into them next month?
I have a
~/containers/vagrant directory with role based sub-directories. For example, I have a build directory for a box with Jenkins, Nexus, and Sonar installed, a mongo, redis, and kafka directory for playing with those technologies - you get the picture. I also keep a README.md at the vagrant level to record port mappings and forwarding and another in each sub-directory that describes the contents, where they came from, and how I have changed the VM.
What is an .md file? Markdown is a plain text markup language that can be easily be converted to other richer presentations like html. It is the standard documentation format in GitHub and seems to be invading other arenas as well. Here is a really sweet editor
Now, to open file from the terminal with something like
macdown README.md, execute this
Box? The Vagrant docs say it well:
Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment.
So, where do I find boxes, and how do I pick one? Hashicorp has a repository of standard boxes at https://atlas.hashicorp.com/boxes/search. You can browse and find one you like. Ubuntu has an official Tasty build that seems pretty reasonable, and provides a solid base for future Docker work.
Now the really interesting part - create and start a new VM. Using the organization described above, we’ll create a “test” VM and start it.
Which will produce
Initialization and startup in 40 seconds. Nice! Subsequent startups on this OS should take 20 seconds. A smaller, more focused OS will take less time.
From the listing we see that we can ssh to the VM on 2200 and that our local directory is mapped to /vagrant on the VM - a very convenient way to share files. Vagrant is packed full of convenience like this, because that is the whole reason it exists. From the “test” directory, you can
and you are in the VM.
You can get an overview of all vagrants with the VirtualBox app - start it up and you should see your “test” VM running. Vagrant provides this summary information as well.
You can manage vagrants from within the vagrant directory or using the id from global-status To stop the VM in the “test” directory
Or, from any directory
To delete the VM and all cached information about, including that held by VirtualBox
Then delete the “test” directory. If you take a peek in VirtualBox, you will see it is missing from VM list as well.