In this post, I describe how to install WSL 2 to serve as the backend for Docker Desktop for Windows. Previously I've always worked with Linux in a Virtual Machine using something like VMWare or Virtual Box, but I decided to give Docker Desktop a try, and was pleasantly surprised by the experience!
With WSL2, it became possible to install and run the standard Linux Docker daemon inside a WSL distribution (distro). This had the benefits of starting up more quickly and consuming a smaller amount of memory on startup, and only increasing the memory consumption when you run containers. This was when I decided that I would try to use the Windows Subsystem for Linux (WSL 2) and the Kali distro from the Microsoft Store instead. Because I stink at Linux (see above) I ran into several issues. Here is a step by step for getting Docker on Kali Linux on WSL 2 on Windows up and running!
I'll start by describing how to install WSL 2 based on the install documents, then show how to install Docker Desktop. Finally, I show the the experience of running a container and how it looks in Docker Desktop.
The Windows Subsystem for Linux (WSL) is a way to run a full Linux environment on your Windows machine, without having to install a 'heavier' virtual machine, such as Virtual Box, VM Ware, or Hyper-V. WSL 2 also provides a mechanism for running Docker (with Linux containers) on your Windows machine. We'll install it as a prerequisite for Docker Desktop for Windows.
If you're running Windows Home, WSL 2 is the only way to run Docker Desktop. On Windows Pro, you can also use Hyper-V, but that is deprecated now, so WSL 2 is the way to go in all cases.
1. Install WSL 1
We'll start by installing WSL 1 by enabling the WSL feature.
Open PowerShell as Administrator (I hit the Start key, type
powershell and hit
ctrl+shift+enter to open an elevated command prompt). Run the following, to enable WSL 1
You should see the following:
That enables WSL, but we're going to enable the newer WSL 2 version, which Docker Desktop can use to run the VM it needs to run containers.
2. Enable the Virtual Machine feature
WSL 2 uses a lightweight VM, so you need to enable the Virtual Machine feature available in Windows 10 version 1903 and higher. In the same, elevated command prompt, run:
You should see something like the following:
Time to restart your machine to ensure everything is registered correctly.
4. Install the WSL 2 update
Download the WSL2 Linux kernel update package for x64 machines and install it.
Note, if you see the following error when running the update, make sure you've restarted your machine afer installing the WSL 1 and VM features:
The installer runs very quickly, and hopefully you'll see a confirmation screen, something like the following:
5. Set WSL 2 as your default version
This is an easy step, just run
wsl --set-default-version 2 in any PowerShell window:
6. (Optional) Install a Linux distribution
We actually don't need to install a Linux distribution to use Docker Desktop, but if you want to shell into Linux directly, you'll need to install one. You can install a distribution directly from the Microsoft Store.
The first time you launch the distribution, the distribution will extract and install, and you'll need to set a username and password.
7. (Optional) Add to Windows Terminal
If you're using Windows Terminal (you should be!) you can configure it to open your WSL distribution. The easiest way to do this is to open up the settings.json file and reset it. You can do this by deleting the contents of the file—Terminal will automatically repopulate it with the defaults, which will include a tab for WSL.
Obviously you won't want to lose your setting customisations, so make sure to backup the contents of the file first!
You're looking for WSL in the
profiles list—add that to you profile list, and you'll be able to quickly open up your WSL instance from Windows Terminal!
That's the prerequisite starter for Docker Desktop covered, so now lets get to the main course!
Installing Docker Desktop for Windows
1. Download and Install
Head to the Docker Desktop website and download the Installer for Windows.. This is a hefty ~500MB, but once it's finished, run the installer.
On the first screen, ensure you keep 'Install required components for WSL 2' checked:
After clicking OK, the installer will unpack a whole load of files:
Before finally prompting you to log out of your Windows account to complete the installation:
2. Logout and back in
The Docker Desktop installer will automatically log you out of your Windows account on completion. When you sign back in, you'll get a notification that Docker is starting the Linux Containers WSL 2 backend. Eventually, the main Docker Desktop window will pop up, and offer you a tutorial
3. Give it a try!
With Docker Desktop installed, you now have the
docker command available in your command prompt:
Now it's installed, let's take it for a spin!
Trying out Docker Desktop for Windows
For simplicity, we'll run one of the .NET 5.0 sample apps available on DockerHub:
This is a simple Razor Pages app, packaged in a Docker image, and ready to run. You can see the Dockerfile for the app here. When you run the above command Docker will pull the image, run it and start listening:
If you navigate to http://localhost:8000/, you'll see the app running there—a Linux container, running on Windows 🤯
The interesting thing, which I didn't realise until I installed it, is that Docker Desktop for Windows installs a very nice UI for managing your containers!
If you click on the
my-sample container row, you'll see the logs for the container:
You can also inspect the environment variables, view stats about the container, or click CLI to open a shell inside (
exec into) the container! You can do all these things from the command line obviously, and if you're automating things, that's definitely the right option. For casual or local development, personally I find you can't beat a nice GUI!
In this post I described how to Install WSL, upgrade it to WSL 2, and how to install Docker Desktop. This requires a restart of Windows and a logout which is a bit annoying, but otherwise it went very smoothly. The Docker Desktop UI also looks very handy, in particular the ability to
exec into a container—I'm always getting the CLI command wrong, so that will be very unseful!
March 2, 2020 by Matt Hernandez, @fiveisprime
Last June, the Docker team announced that they will be investing in getting Docker running with the Windows Subsystem for Linux (WSL). All of this is made possible with the recent changes to the architecture of WSL to run within a lightweight virtual machine (VM), which we talked about in an earlier blog post about WSL 2. Since this announcement, the Docker team has released a Technical Preview of Docker that includes support for running with WSL 2.
This article explains how the Docker Desktop technical preview works as well as how to use the Docker extension with the technical preview.
How it works
This new Docker architecture works a lot like Visual Studio Code's WSL remote development support in that the Docker CLI running on the host machine executes commands within the Docker Integration Package, which runs on the remote WSL VM.
Image credit: Docker Engineering
DockerD runs directly within WSL so there's no need for the Hyper-V VM and all Linux containers run within the Linux userspace on Windows for improved performance and compatibility.
Getting set up
First some prerequisites:
- Install Windows 10 Insider Preview build 18975 (Slow) or later for WSL 2.
- Install Ubuntu from the Microsoft store.
- Enable WSL 2 by following this guide.
- Install the Remote - WSL extension for VS Code.
- Install the Docker WSL 2 Technical Preview.
Once installed, Docker will recognize that you have WSL installed and prompt to enable WSL integration. You want to Enable WSL integration for this tutorial.
This option will allow you to access Docker Desktop via the Docker CLI directly from within your Linux distro.
If you have multiple Linux distros, make sure you only have WSL integration turned on for the correct one in your Docker settings:
With that configured, all commands will execute in the Linux context - this includes Docker commands run from PowerShell so running something like
docker run mongo… will start a Linux container within the WSL VM.
docker ps command over in WSL, you'll see the container as expected. Notice that the container ID matches.
Using VS Code
With this set up and running, you can install the VS Code Docker extension and access your containers. If you're already running WSL 2 and the Remote - WSL extension, this will help you get Docker integrated into your WSL workflow rather than switching contexts when you need containers. And because the Docker CLI's context is set to use DockerD in WSL, the extension will work with your containers regardless of whether you opened VS Code using the Remote - WSL extension.
Notice how in the screenshot below, I'm connected and working in WSL and still building/running containers without changing from my preferred environment (zsh in Ubuntu).
Theme: Noctis Sereno
Install Docker On Linux Subsystem For Windows
I've personally noticed a vast improvement in container execution times using this configuration and each part of my typical development workflow remains the same. I'm also using the Remote - Containers extension within WSL for testing specific environments without setting things up directly on my machine.
We want your feedback
Keep in mind that you're using prerelease software and, while the Windows Insiders Slow ring is very stable, you may run into some issues. If you do find something that isn't working as expected, please open an issue via the Feedback tool in Windows. Any direct Docker issues or feedback can be logged in the Docker for Windows repo.
Install Docker On Linux Subsystem For Windows
Matt Hernandez, VS Code Program Manager @fiveisprime
Comments are closed.