Developing software in the Real World

First steps with Windows on Vagrant

One thing that would be useful for one of my projects is an easy way to install IIS, PHP & SQL Server on Windows in a repeatable way. I wasn’t sure how to do this until I came across Chocolatey and Boxstarter. Chocolatey is a package manager for Windows along the same lines as yum and apt for Linux and knows how to install a remarkable amount of software. Boxstarter is a way to automate installations of Windows using Chocolatey packages.

This is the start of my journey and I’ve not yet reached the destination I’m hoping to get to.

The Windows Vagrant base box

To start out I need a Windows base box for Vagrant. There doesn’t seem to be an obvious frequently-updated Windows Server 2016 Vagrant box out there, so I turned to Matt Wrock‘s article Creating windows base images using Packer and Boxstarter which provides a great overview on what to do. He even provides a GitHub repository of packer-templates which do the hard work.

I forked his repo and tweaked a few bits and then ran packer on my Mac and it generated a windows2016min-virtualbox.box base box.

To install into Vagrant, you can add to the Vagrant Cloud so that it’s easily shareable, or you can just add to your local vagrant with:

For myself, I named my box 19ft/windows2016.

Vagrantfile

The initial Vagrantfile is quite simple:

We can then start our VM:



This will fire up Virtualbox with Windows 2016 in a GUI that you can log into. There are two users: Administrator and Vagrant, both have the same password: vagrant.

You may want to turn on VirtalBox’s shared clipboard from the Devices menu.

The next step is to automate installing some software on it.

Automating software installation

We can use Chocolatey controlled by Boxstarter install useful software.

Firstly we need to add a provisioning script to our Vagrantfile:

If we vagrant destroy the the current VM and then run vagrant up again, Vagrant will run our inline provisioning script which will install Boxstart which will then run setup.ps1

setup.ps1 is the script that contains the instructions for setting up Windows and installing various software. My initial one looks like this:


When the script runs it sets some configuration options on Windows Explorer and then install Chrome. Chrome requires a restart, so Boxstarter notes that a restart is required, so will store the information required in order to start up again after the reboot and then initiate the reboot.

Vagrant doesn’t handle this well and will fail with a message something like this:

Don’t worry though, the VM will restart and Boxstarter will continue. As there’s no way to monitor this through Vagrant, having the GUI available is useful so we can watch it work. Boxstarter will the go on and install a Firefox, NotePad++, git, SQL Server, IIS and PHP for me with no intervention required.

Note that provisioning takes a long while. At the end though, we have Sql Server and IIS/PHP installed. We can then use vagrant package if we want to store the state at this point should we wish to.

All done

I’m a fan of scripted set up of infrastructure as I’m much more likely to be able to repeat it and share with team members. With Boxstarter and Chocolatey, it has been a nice surprise that I can build a Windows Vagrant VM mostly automatically in the same way I can with Linux.

It’s not pain-free though as it seems far less robust and sometimes I have to re-run the boxstarter script which is irritating, but not the end of the world.

The full Vagrantfile and setup.ps1 are in this gist.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *