Using Linux containers for software development has many benefits. For instance, it allows you to quickly and simply run multiple, different versions of a database engine like MongoDB or SQL Server concurrently. You can also easily get project-specific code, such as APIs and integration tests, running locally in a matter of seconds with a few commands. The benefits go on.
Docker is a well-known front end for Linux containers, and it works on Windows, Linux, and MacOS. From the Docker website: “Docker simplifies and accelerates your workflow while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.”
My own experience with Docker has been positive: it’s easy to use and has been very beneficial to my software projects. Docker allows us to easily spin up the appropriate tools for the appropriate problem with very little overhead. For example, I was working with a client who had a MongoDB instance that held data for different services. It was very easy just to start a Docker MongoDB container and have the setup we needed, instead of going through the process of installing MongoDB locally and managing versions. This also gave us the advantage of turning it off locally when we didn’t need it for the particular code we were working on.
Docker Desktop for Windows initially used heavy, full Virtual Machines on Hyper-V to run Linux containers. This isn’t necessarily bad, but it does cost overhead in speed and interoperability between the container and the Windows host.
With the evolution of Windows Subsystem for Linux (WSL) from WSL 1 to WSL 2, Docker Desktop can now take advantage of a real, full implementation of the Linux Kernel in a lightweight virtual machine giving your containers speed and more seamless integration with Windows.
What is WSL 2 and what does it do for Docker Desktop?
WSL 2 is the current version of Windows Subsystem for Linux, Microsoft’s tools that allows developers to run a GNU/Linux environment directly on Windows. The goal of WSL is to provide a seamless bridge between Windows and Linux. This allows developers to use whatever environment they want and still work effectively as a team. For example, build scripts written on Debian can be run on WSL. With WSL, you can run several distributions of GNU/Linux right on Windows, including Ubuntu, Debian, and Kali.
WSL 2 takes the greatness of WSL 1 and supercharges it by giving it a full Linux kernel, specially modified and designed by Microsoft to boost interoperability between the two systems, running on a lightweight, hands-off virtual machine. Microsoft says this is a different VM experience with better performance and the same great connected experience WSL 1 gives us. Another especially interesting feature is the remote capabilities that WSL 2 offers, especially the integration with Visual Studio Code. A full list of differences and new features of WSL 2 can be found here.
The new features and architecture of WSL 2 make running containers with Docker Desktop faster and more efficient. Now, containers can boot in seconds and leverage the advantages of the full kernel. In addition, memory and CPU consumption is down. According to Docker themselves, “it takes less than 10 seconds to start the Docker daemon when compared to almost a minute in the previous version of Docker Desktop.”
Setting up WSL 2 and Docker Desktop
Let’s look at how to make Docker and WSL 2 work for you by walking through the setup process.
Installing WSL 2 is straightforward. It’s even simpler if you have the Windows OS build 202562 or higher, which is part of the Windows Insiders Program at the time of this writing.
If you do have build 202562, you simply type the following in an Administrator command line window and restart:
This command does everything for you: enables WSL, installs Linux kernel, sets WSL 2 as your default environment, and even downloads and installs a Linux distribution.
If you are not a part of the Windows Insiders Program, here are the steps to setting up WSL 2.
- Enable WSL
- Update to WSL 2
- Enable VM feature
- Download the Linux kernel update
- Set WSL 2 as your default
- Install your Linux distro of choice from the Microsoft Store
- Restart Windows
You can find the details on installing WSL 2 from Microsoft here.
Once you have WSL 2 set up and running, you can get Docker Desktop working with it and enjoy better performance and interoperability. The following are the steps
- Get Docker Desktop from here and install following their instructions. After Docker Desktop is installed it’s just a matter of settings.
- Open the settings from your system tray.
- Under “General”, check the “Use the WSL 2 based engine” setting.
- In Resources -> WSL INTEGRATION check the “Enable integration with my default WSL distro” setting. You can also enable it for all your distributions if you have more than one.
- Finally, reboot your computer and you have Docker Desktop with all the WSL 2 advantages. You can create new containers inside of your Linux distribution or on Windows itself and Docker will use WSL 2.
Bask in the benefits
Now that WSL 2 is set up and Docker Desktop is installed and configured to use WSL 2, your containers—and consequently your development—have been supercharged. The improved development experience you’re about to deliver will lead to faster and more high-quality throughput. In turn, this will provide more stable systems for your customers while delivering cost savings and improved overhead for your business.