Improve Docker performances with WSL2 | Towards Data Science

Docker was claimed as the leading solution for setting up a local development environment. Thanks to the simplicity of docker-compose files, you can have an isolated environment per project, that reflect the same configuration of the production environment.
Moreover, this solution makes the development independent by the operative system you have. In poo word, even if your container will be a Linux image, you can develop by using Windows or Mac.
Anyway, in some setup the performance may decrease, compromising the productivity.
Form last Docker update, Docker engineers added the possibility to use WSL2 technology for boosting performances, giving same development experience on all platform, windows included.
This article explains why we may have performance issues on Windows, and how to install WSL2 properly for improving performances.
Docker is the leading container solution, active since 2013. I don't want to spend a lot of time talking about what docker is. If you are here, probably already use it, so I will spend just a few words for introducing the limitation that WSL2 overcome. Anyway, if you want to know more about docker, you can read this article where I tried to explain in simple words“what is exactly docker”.
The central part to remark about Docker is how it works under windows. Because it shares the OS, not the hardware, you couldn’t have any Linux based containers on Windows. That is. But everybody uses Linux containers on windows, how it is possible? Docker engineers solved this problem with a trick. We cannot have a Linux container on Windows, but we can put Linux in windows machine by using a hypervisor. So the solution was quickly coined. Docker Desktop for windows use a Hypervisor (usually Microsoft HyperV) for running a virtual machine, and it shares the VM OS to the docker container. That’s how things work, and I tried to explain it using a diagram.
The big problems with this architecture are:
the resource limitation. While docker is designed for sharing OS, a hypervisor still use hardware, and you need to allocate resources to it. This lead to the same performance issues we had using virtual machines.
Disk overhead. The container image is inside the VM, it stores files in volumes, but you may want to share folders from the Windows environment, especially when developing. This is the red-arrow roundtrip in the diagram and its impact performances.
As Docker on Windows for running Linux container if used mostly in a development environment, performance doesn't matter a lot. In fact, most of the solution requires low resource for handling one request at a time. Anyway, there are some cases when this performance matters.
Platform with an application states, where the executable is compiled and loaded once, doesn't offer too much about disk latency. Developing in .net or java, you just have to press the play button, wait a minute, and then you have your application ready for tests, where all the compiled source code is more or less in RAM, and you don't need to stress the hard drive.
Instead, for PHP or Python application, it’s different. As they haven’t any application state, each request is interpreted independently, so you need to scan and load all the files required each time. This means a lot of file read, so your latency is multiplied with an immense amount of file.
For this system, using volume mapping on windows is quite expensive and decrease performance. You could use volume mounted to the Linux OS, but in this case, you will have trouble on editing from the Windows UI.
That’s why, in most cases, we accepted the slowest performance (in most cases acceptable for a dev environment), or in other cases we were forced to switch to Linux, nullifying the advantages of using Docker for having multi-OS availability.
WSL (Windows Subsystem for Linux) is a compatibility layer for running Linux binary executables natively on Windows. It works since Windows 10 and let you use Linux inside Windows without using virtual machines. Since June 2019, an enhancement of WSL (WSL 2) was delivered with Windows 10. This update brings a lot of performance improvement through a set of optimised Hyper-V features.
In simple words, you have now a Linux distribution inside your Windows machine faster than running virtual machines.
The good news is that Docker Desktop supports it and it can boost your containers.
Using WLS2 and Docker combines, we virtually skip one step.
The Windows and Linux file system are mutually shared: you can see Linux from windows accessing the shared drive \\WSL$\ or by Linux you can see Windows from /mtn/c)
The docker containers use the Linux WSL without any resource limitation and have better performance.
In this scenario, there is huge bad news. Sharing windows folder is still possible, but with worst performance than in the standard case. This is not a problem in Java or .net, or a tolerable problem, but made the PHP development unfeasible.
Fortunately, there is a solution that mixes the benefit of using WSL2 and a great development experience. In fact, Visual Studio Code is able to work remotely to the WSL2 subsystem so that you can do the following:
Keep the volumes inside WSL2 as regular volume mount, so get the best performance for file system access.
Work from Windows, as usual, editing the code on Linux.
It’s clear that if you will pay a more significant cost for accessing the file while you double click an item in your IDE or save it, it is a very acceptable solution. The problem comes with massive file load, and this is not the case of a developer that writes code working on a dozen of time simultaneously.
Anyway, Visual Studio does somethings better. It has an extension that can interact remotely with their remote subsystem, so you can keep the UI into Windows, but use the engine from an instance inside Linux. So, also, your user experience as a developer is safe.
Well, once we have understood how and why we can boost performance on Docker, we just need to follow some easy step for getting things done.
We will need to perform a couple of steps, but, from a higher point of view, we have to:
Enable the WSL2 environmentConfigure the IDE and start coding.
Enable the WSL2 environment
Configure the IDE and start coding.
This section explains how to install the WSL2 version an be ready for starting coding with Docker and WSL2.
This first step enables the Window subsystem. You have to run it on a command line with administrative rights.
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
This second command activates the Virtual Machine Platform (yes, WSL still uses hyper-v behind the hood). If your PC doesn’t have the virtualisation feature turned on, you may need to set it from the BIOS.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
After this second step, a reboot is welcome.
The last step for installing and configuring WSL2 is to install the update. It is a simple MSI package, so you can download it and install with a double click.
The file is hosted here from Microsoft.
Once we are ok with WSL2, just set up version 2 as default for all the subsystem.
wsl --set-default-version 2
To run Docker over a subsystem, we need to download a Linux distro. Since Microsoft + Open Source is now a reality, it is possible directly from the Microsoft store. I used Ubunto, and it is just 440MB, so don’t worry about finish your disk space.
After this step, you will see all the OS installed as a subsystem.
wsl --list --verbose
You should see docker, docker-data, ubuntu, all on WSL 2.
Inside Docker Desktop, just check the configuration and enable the WSL2 feature. It may need some docker restart or PC restart.
Warning: When you switch from standard mode to WSL2, you will need to redownload all the images (that you have located inside the Hyper-v VM instead of WSL2). Moreover, the data inside the named volumes are stored inside the VM, so you won't find them in the new WSL2 machines. So, first of all, extract and backup all relevant data from the Docker container. Basing on your local setup, you may lose data.
This first step is to check “Use the WSL2 based engine”.
Then you unlock the WSL integration panel, where you can activate Ubuntu distro.
The last step is to install and download the Visual Studio Plugin. As usual, you can do it by clicking install from the web site or by the Visual Studio Code UI.
You can also find the extension here.
At that time, all the steps are completed, and you can start coding. You will thank this time spent when you will see the performance! The final balance will be a lot of time saved just on the first day of work.
For start coding, you have two options: starting from the WSL or from Windwsow.
Personally, I prefer the second one as it is very self-contained.
1. Open WSL terminal or enter the command “WSL” into a shell. As Ubunto is the preferred subsystem, you will be inside it.
2. Type “code .” This will open Visual Studio Code on your windows machine.
1. Open visual studio code.
2. Click the WSL box (left bottom corner, the green button into the bottom toolbar) or F1 + Remore WLS+ New Window
3. the new window is connected with the Linux OS
Since that moment you can use Visual Studio Code to do all you need. In fact, you can open a terminal from the file tree. The terminal will be inside the WSL machine, and if you run docker-compose up, you will activate containers on the host docker system. You can also use the git tools for handling commits, so probably that’s all you need to start coding.
For many reasons, a lot of developers are using Windows as the development environment. This is a common choice for having best programs compatibility or a better user experience that Linux. Other are using Windows for getting cheaper notebook than Mackbook.
Docker helped for having the same development experience on all the OS system, but in some case may lead to performance issues on Windows.
WSL2 improve performance but need some attention about where to put the file and how to create volumes.
Once these steps are done, you can benefit from the same performance you had in Linux, but with your comfortable Windows user experience.
Disclaimer: I don’t want to animate the sectionalism between Linux, Windows and Mac supporters. Each developer should be free to choose the OS that prefers, having the same user experience. This is not the main purpose of Docker but is a comfortable side effect. The purpose of this article is just to unlock a new opportunity and avoid a forced OS change for who prefer working on Windows.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.
daniele-fontani.medium.com
References:
Official docker guide for using docker with WSL2
Visual Studio Code remote development architecture