Using Packer and Vagrant to Build Virtual Machines
UPDATE: We are delighted that Mitchell Hashimoto, the creator of Vagrant and Packer, shared our blog post and recommended reading it. Retweet to share the love.
"Building Vagrant Machines with Packer", by @codeship. Good intro to how and why Packer is useful with Vagrant. http://t.co/Rp1VUzhE36
— Mitchell Hashimoto (@mitchellh) November 7, 2013
Sharing a common development environment with everyone on your team is important. It is really hard though to keep the same dependencies, database versions and other systems in sync between different machines.
Vagrant is a great tool that helps with this and manage the lifecycle of a virtual machine. As nice as Vagrant is, provisioning machines with it has always been a pain. A couple of months ago Mitchell Hashimoto, the creator of Vagrant, launched Packer.
Packer lets you build Virtual Machine Images for different providers from one json file. You can use the same file and commands to build an image on AWS, Digital Ocean or for virtualbox and vagrant. This makes it possible to use exactly the same system for development which you then create in production.
In this blog post we will show you how you can use Packer to build your vagrant machines. In a follow up post we will focus on how we use Packer for building all of our Continuous Deployment & Integration Infrastructure.
Prerequisites for building Vagrant Machines
You need Virtualbox and Packer installed. Virtualbox provides packages for different Operating systems. Packer is even easier, just download the right zip for your system and unzip it into your PATH
Building your Virtual Machine with Packer
We've collected all the files necessary to build a Vagrant Machine with Packer in our Packer Example repository.
Packer uses builders, provisioners and post-processors as the main configuraition attributes. A builder can for example be virtualbox or AWS. A provisioner can be used to run different scripts. Post-processors can be run after the machine image is done. For example converting a Virtualbox image into a suitable image for vagrant is done in a post-processor.
Here is the main packer.json file. You can see the builder, provisioner and post-processor defined:
It builds for virtualbox and then exports it into vagrant. The http folder contains a preseed.cfg file that is necessary to set up Ubuntu.
In the scripts folder you can find a root_setup.sh and setup.sh scripts.
The root_setup.sh script sets up necessary packages and parameters for Vagrant:
The setup.sh script install different dependencies like ruby or redis to set up the virtual machine exactly how you need it:
You don't have any limits what you can run in your virtual machine through these scripts.
Building the Machine
We've added a create_box script that makes it easy for you to get started
You will then see Virtualbox start up and build the machine
Run the script and it will create the packer machine and import it into vagrant. Then all you have to do is run
And you have your development environment set up.
You can now get into the machine with
vagrant ssh and start coding.
Vagrant is an incredibly powerful tool and together with Packer it is easy to build development environments for your whole team.
But this is only the beginning. Packer can go much further than just providing your development environment. We are currently implementing Packer as the tool to build all of our test infrastructure servers. This new set of tools is great for Immutable Infrastructure and Continuous Deployment so you can build more stable, secure and easy to change infrastructure than ever before.
Let us know in the comments how you use Packer and Vagrant. We are excited to hear your thoughts!
Other Codeship posts you may also find interesting:
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.