Docker Desktop and WSL are two popular tools for developing and running containerized applications on Windows. Docker Desktop is a Docker client that provides a graphical user interface for managing Docker containers. WSL is a Windows feature that allows you to run Linux distributions on Windows.
WSL Vs WSL 2
The main difference between WSL and WSL2 is the architecture. WSL1 used a translation layer to convert Linux system calls to Windows system calls, while WSL2 uses a lightweight virtualization layer to run a real Linux kernel. This gives WSL2 a number of advantages over WSL1, including:
- Better performance: WSL2 can run Linux applications at native speed, without the overhead of the translation layer.
- Full system call compatibility: WSL2 supports all Linux system calls, including those that are not available in Windows. This allows you to run a wider range of Linux applications on WSL2.
- Better GPU support: WSL2 supports hardware acceleration for GPUs, which can improve the performance of graphics-intensive applications.
- Better support for Docker: WSL2 makes it easier to run Docker containers on Windows.
Here is a table that summarizes the key differences between WSL and WSL2:
Overall, WSL2 is a significant improvement over WSL1. It offers better performance, full system call compatibility, better GPU support, and better support for Docker. If you are using WSL for development or production workloads, it is recommended that you upgrade to WSL2.
Configuring Docker Desktop with WSL 2
To configure Docker Desktop with WSL 2, follow these steps:
- Download and install the latest version of Docker Desktop for Windows.
- Turn on WSL 2 – Depending on which version of Windows you are using, Docker Desktop may prompt you to turn on WSL 2 during installation.
- Start Docker Desktop from the Windows Start menu.
- Navigate to Settings.
- From the General tab, select Use WSL 2 based engine..
- If you have installed Docker Desktop on a system that supports WSL 2, this option is turned on by default.
- Select Apply & Restart.
- Now docker commands work from Windows using the new WSL 2 engine.
How it works?
Docker Desktop installs two special-purpose internal Linux distros – docker-desktop and docker-desktop-data.
The first (docker-desktop) is used to run the Docker engine (dockerd) while the second (docker-desktop-data) stores containers and images. Neither can be used for general development.
WSL 2 adds support for "Linux distros" to Windows, where each distro behaves like a VM except they all run on top of a single shared Linux kernel.
Docker Desktop does not require any particular Linux distros to be installed. The docker CLI and UI all work fine from Windows without any additional Linux distros. However for the best developer experience, we recommend installing at least one additional distro and enable Docker support:
Ensure the distribution runs in WSL 2 mode. WSL can run distributions in both v1 or v2 mode.
To check the WSL mode, run:
$ wsl.exe -l -v
To upgrade the Linux distro to v2, run:
$ wsl.exe --set-version (distro name) 2
To set v2 as the default version for future installations, run:
$ wsl.exe --set-default-version 2
When Docker Desktop starts, go to Settings > Resources > WSL Integration.
The Docker-WSL integration is enabled on the default WSL distribution, which is Ubuntu. To change your default WSL distro, run:
$ wsl --set-default <distro name>
Select Apply.
Reboot Windows
Now docker commands work from Windows using the new WSL 2 engine.
Run the hello-world container
docker run hello-world
Build and run a Docker image from a Dockerfile
docker build -t my-image .
docker run my-image
You can also use Docker Desktop to manage Docker containers running in WSL. To do this, open Docker Desktop and select the "WSL" tab. This will show a list of all the Docker containers running in WSL. You can then use Docker Desktop to start, stop, and manage these containers.
Code sample
Get a list of all the Docker containers running in WSL
docker ps -a
Start a Docker container running in WSL
docker start my-container
Stop a Docker container running in WSL
docker stop my-container
Remove a Docker container running in WSL
docker rm my-container
By following these steps, you can configure Docker Desktop to work with WSL and start developing and running containerized applications on Windows.