It is 2021 — running Docker and Vagrant parallel on Windows isn’t impossible anymore
Do you have some legacy projects based on Vagrant and now you would like to slowly migrate to Docker? It is super easy if you’re a user of Linux — but what if your PC is running Windows? Some time ago it would be a “well… good luck” situation — it was difficult to use Vagrant (with VirtualBox) and Docker parallel. But hey — we’re in 2021 now, is it still so hard to make it work?
Short answer — no. It is a piece of cake to run Docker and Vagrant at the same time on Windows. Yes, even if you have Windows 10 Home Edition.
The main problem why running Docker and Vagrant was so tricky is the Hyper-V hypervisor which is required be Docker to work. I won’t dive deep into “what Hyper-V is” but what you need to know is that when you try to enable this function in Windows, your Vagrant won’t work — that’s because VirtualBox (which is used by Vagrant in most cases) doesn’t like Hyper-V. Well… that was true some time ago, but now? VirtualBox 6.x works with Hyper-V pretty well!
You could also hear that Windows 10 Home Edition isn’t able to run Docker at all. Why? Of course because of Hyper-V — to be more precise because of lack of support for it. But now we have WSL2 with HV support, even for Windows 10 HE.
What does WSL2 stand for? Windows Subsystem for Linux — to make it simple let’s say it is Linux Kernel inside your Windows System. You can find more on official Microsoft pages if you want to — for now you can think it is Linux where your Docker can run. Some people will be triggered by that simplification, but I think it is enough close to the true
When you try to find some information in Google, you’ll find a lot of old articles that claim it is impossible to run Vagrant and Docker on Windows at the same time. If you’ll have some more luck you’ll find solutions with Docker Toolbox (which is deprecated) or with switching hypervisorlaunchtype flag :
# disable Hyper-V to use VirtualBox/Vagrant — it brokes Docker
bcdedit /set hypervisorlaunchtype off# enable Hyper-V to use Docker — it brokes Vagrant/VB
bcdedit /set hypervisorlaunchtype auto
The hypervisorlaunchtype solution is pretty nice but:
- you can’t have Docker and Vagrant running at once — you have to choose what tool you need at that very moment
- you have to restart Windows each time when you decide to switch hypervisorlaunchtype flag
Probably you won’t be arguing with me when I say it isn’t a perfect solution.
First of all, make sure you have enabled virtualization in your BIOS setup. You should also update Vagrant and VirtualBox to the newest versions. To check if your Vagrant is working, for example:
vagrant init hashicorp/bionic64
In my case, I received the “VT-x is disabled in the BIOS for all CPU modes” error. It seems to be a pretty self-explanatory error — but I checked and I had VT-x enabled in BIOS. To fix this error I had to set the “OS features” setting in my BIOS at the “Windows 8 and 10” option. I had this issue only on one of my machines so I assume it is a rare case.
Quick tip: to open BIOS in Windows 10 machine go to system’s Settings -> Update and Security -> Recovery -> Advanced startup -> Restart now. Your machine will restart to Startup Mode where you should choose Troubleshoot -> Advanced options -> UEFI Firmware Settings
At this point, you should have running Vagrant.
Let’s install Docker.
Go to https://www.docker.com/get-started page and download Docker for Windows. During installation make sure you have the “WSL2 support” checkbox marked
If you don’t have installed WSL2 in your system Docker will inform you that this is required to work.
Linked MS documentation explains in details how to install WSL2 — in most cases you don’t have to go thru the whole process, it is enough to do only Step 4 and Step 5:
- Download the Linux kernel update package
- Set WSL 2 as your default version
After Windows restart, Docker should work.
What if you have Docker installed before?
Just make sure you’re using Docker for Windows (not Docker Toolbox) and you have enabled WSL2 support in settings.
If you were using the “hypervisorlaunchtype” trick before, make sure you have enabled it by running this in your CMD (as administrator)
bcdedit /set hypervisorlaunchtype auto
Moment of truth
Let’s check if everything is working. Try to run some Docker container:
docker run -dit ubuntu
If the docker container successfully runs you should see output like this:
Great, let’s try to run Vagrant. Open your console somewhere where your Vagrantfile is (if you doesn’t have any, use vagrant init hashicorp/bionic64 to create one) and try run Vagrant:
If your Vagrant will turn on without a problem — you’re done. But probably it will fail and you will end with an error:
Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured (“config.vm.boot_timeout” value) time period.
In that case, open your Vagranfile and set Paravirtualization Interface to Hyper-V (make sure your VirtualBox version is 6.0 or above):
Clear cache, and try to re-run Vagrant — everything should work now:
rmdir /s .vagrant
I know this article is basic and doesn’t cover every possible issue you can face in Vagrant/Docker configuration. My goal wasn’t to create a complex tutorial that covers everything but to show you that in 2021 Docker and Vagrant can be used together without pain, even on Windows machines… Well, in most cases ;)