Vagrant



A vagrant is a common tool for virtual machines management combined with a simple command line interface. The tool is expected to make the entire process easy without polluting the main installation with tons of configuration files. In the backend, there is one Virtual Box utilized by the Vagrant tool to run virtual machines. If any of these are specified, Vagrant will emit a warning, but will otherwise boot the AWS machine. There is minimal support for synced folders. Upon vagrant up, vagrant reload, and vagrant provision, the AWS provider will use rsync (if available) to uni-directionally sync the folder to the remote machine over SSH.

Vagrant

Vagrant Queen

-->Vagrant

A few months ago, I went to DockerCon as a Microsoft representative. While I was there, I had the chance to ask developers about their favorite tools. The most common tool mentioned (outside of Docker itself) was Vagrant. This was interesting -- I was familiar with Vagrant, but I'd never actually used it. I decided that needed to change. Over the past week or two, I took some time to try it out. I got everything working eventually, but I definitely ran into some issues on the way. My pain is your gain -- here are my tips and tricks for getting started with Vagrant on Windows 10 and Hyper-V. NOTE: This is a supplement for Vagrant's 'Getting Started' guide, not a replacement.

Tip 0: Install Hyper-V

VagrantVagrant

For those new to Hyper-V, make sure you've got Hyper-V running on your machine. Our official docs list the exact steps and requirements.

Tip 1: Set Up Networking Correctly

Vagrant doesn't know how to set up networking on Hyper-V right now (unlike other providers), so it's up to you to get things working the way you like them. There are a few NAT networks already created on Windows 10 (depending on your specific build). Layered_ICS should work (but is under active development), while Layered_NAT doesn't have DHCP. If you're a Windows Insider, you can try Layered_ICS. If that doesn't work, the safest option is to create an external switch via Hyper-V Manager. This is the approach I took. If you go this route, a friendly reminder that the external switch is tied to a specific network adapter. So if you make it for WiFi, it won't work when you hook up the Ethernet, and vice versa. [caption align='aligncenter' width='879'] Instructions for adding an external switch in Hyper-V manager[/caption].

Tip 2: Use the Hyper-V Provider

Unfortunately, the Getting Started guide uses VirtualBox, and you can't run other virtualization solutions alongside Hyper-V. You need to change the 'provider' Vagrant uses at a few different points. When you install your first box, add --provider:

And when you boot your first Vagrant environment, again, add --provider. Note: you might run into the error mentioned in Trick 4, so skip to there if you see something like 'mount error(112): Host is down'.

Vagrant Status

Tip 3: Add the basics to your Vagrantfile

Vagrant Synonym

Adding the provider flag is a pain to do every single time you run vagrant up. Fortunately, you can set up your Vagrantfile to automate things for you. After running vagrant init, modify your vagrant file with the following:

Vagrant Boxes

One additional trick here: vagrant init will create a file that will appear to be full of commented out items. However, there is one line not commented out: [caption align='aligncenter' width='879'] There is one line not commented.[/caption] Make sure you delete that line! Otherwise, you'll end up with an error like this:

Trick 4: Shared folders uses SMBv1 for hashicorp/bionic64

For the image used in the 'Getting Started' guide (hashicorp/bionic64), Vagrant tries to use SMBv1 for shared folders. However, if you're like me and have SMBv1 disabled, this will fail:

You can check if SMBv1 is enabled with this PowerShell Cmdlet:

If you can live without synced folders, here's the line to add to the vagrantfile to disable the default synced folder.

If you can't, you can try installing cifs-utils in the VM and re-provision. You could also try another synced folder method. For example, rsync works with Cygwin or MinGW. Disclaimer: I personally didn't try either of these methods.

Tip 5: Enable Nifty Hyper-V Features

Vagrant Boxes

Hyper-V has some useful features that improve the Vagrant experience. For example, a pretty substantial portion of the time spent running vagrant up is spent cloning the virtual hard drive. A faster way is to use differencing disks with Hyper-V. You can also turn on virtualization extensions, which allow nested virtualization within the VM (i.e. Docker with Hyper-V containers). Here are the lines to add to your Vagrantfile to add these features:

There are a many more customization options that can be added here (i.e. VMName, CPU/Memory settings, integration services). You can find the details in the Hyper-V provider documentation.

Tip 6: Filter for Hyper-V compatible boxes on Vagrant Cloud

You can find more boxes to use in the Vagrant Cloud (formally called Atlas). They let you filter by provider, so it's easy to find all of the Hyper-V compatible boxes.

Tip 7: Default to the Hyper-V Provider

Vagrant queen

While adding the default provider to your Vagrantfile is useful, it means you need to remember to do it with each new Vagrantfile you create. If you don't, Vagrant will trying to download VirtualBox when you vagrant up the first time for your new box. Again, VirtualBox doesn't work alongside Hyper-V, so this is a problem.

You can set your default provider on a user level by using the VAGRANT_DEFAULT_PROVIDER environmental variable. For more options (and details), this is the relevant page of Vagrant's documentation. Here's how I set the user-level environment variable in PowerShell:

Again, you can also set the default provider in the Vagrant file (see Trick 3), which will prevent this issue on a per project basis. You can also just add --provider hyperv when running vagrant up. The choice is yours.

Wrapping Up

Those are my tips and tricks for getting started with Vagrant on Hyper-V. If there are any you think I missed, or anything you think I got wrong, let me know in the comments. Here's the complete version of my simple starting Vagrantfile: