If you have used either WSL or Docker you’ve already experienced some magic of virtualization. With Windows 10 introducing WSL2 you can now run Docker image from Ubuntu running via WSL2 that’s connected to your Host’s Docker Desktop app. That’s just insane! It’s inception in OS level.
Today we will take a look at how to turn on this integration, so you can run Docker inside Ubuntu that’s running inside Windows 10 via WSL2. We will also explore and understand a little magic behind the scene and look at how things are working behind the scene.
First, to enable Docker for Ubuntu on Windows via WSL2 you need to go to Docker Desktop > Settings > Resources > WSL Integration
Make sure you have “Enable integration with my default WSL distro” selected and also turn on each distro (if you have multiple Linux running). For me, I only have Ubuntu installed via WSL2 so that’s the only option shown here.
Once we have it turned on you can launch Ubuntu and without even installing “docker” in Ubuntu you should be able to run the “docker” command. If we run:
docker ps -a
You will all standard docker command function exactly the same as Windows and you will see the print out of the existing docker container, image name and all the details you find from Docker Desktop’s Images tab. Side-by-side it’s quite astonishing to see that working.
So what’s really happening behind the scene?
Let’s run this command and you will get a sneak peak of what’s going on.
In PowerShell, if you run the above command it prints out all the Windows Subsystem for Linux Distributions. But check this out, it prints Ubuntu, docker-desktop-data and docker-desktop. We only installed Ubuntu as a Linux distro, but never actually ‘installed’ the other distro.
The reason how it works is that underneath Docker runs and uses WSL2 to power it’s virtualization. As far as Windows is concerned Docker is just another distro that runs on top of Linux’s kernel.
The integration between Docker Desktop really enables different distro to talk to each other so in our case, Ubuntu was able to run and execute commands that affect the other VM running inside the docker distro. Microsoft has a full document that goes over into more detail on how it works behind the scene. Check it out if you are interested!