Docker with WSL2 based on Windows

Docker with WSL2 based on Windows

WSL (Windows Subsystem for Linux) – is a Windows subsystem that can allow running Linux applications on computers without installing a separate virtual machine or different operating system.

WSL2 works on the basis of the hypervisor. This in turn allows us to have Linux kernel inside a virtual machine on a desktop under the management of Windows. Using the hypervisor significantly reduces overhead costs, which has improved the performance and compatibility of Linux applications.

The difference WSL2 vs WSL1

FunctionWSL 1WSL 2
Integration between Windows and Linux
Fast boot times
Small resource footprint compared to traditional Virtual Machines
Runs with current versions of VMware and VirtualBox
Managed VM
Full Linux Kernel
Full system call compatibility
Performance across OS file systems
For more information, you can read here

Docker and WSL2

WSL provides us with opportunities of using the Docker Desktop for Windows. To be able to use it, we don’t need to have a virtual machine.
With the arrival of WSL2, a lot of bugs have been fixed for Docker and its networks, and the performance of kernel and Docker containers.

WSL2 is available only for thouse who have Windows 10 and higher.

Nowadays, there aren’t any issues using WSL2 with Docker. It works like clockwork WSL2 with Docker.

Installing WSL2, Docker

# check current WSL version
wsl -l -v

# if we have WSL1 (you will see VERSION 1), we need to change WSL1 to WSL2.
wsl --set-default-version 2

# If you already have a <Ubuntu-20.04>, you should to run:
wsl --set-version Ubuntu-20.04 2

# now if you check your WSL version you will see "VERSION 2"
wsl -l -v

# Close power shell

  • At the terminal, let’s run the next command to update the list of available packages and install Docker, ZIP, Git, Make, and Tree packages:
#Update packages sources
sudo apt update

#Install packages
sudo apt install zip unzip git tree docker make
  • Now, inside the WSL2 we have the installed Docker
    • You need to check that the Docker works correctly. Run one of two command below:
      docker --version or sudo docker --version
    • You should see approximately console output:
      Docker version 24.0.2, build cb74dfc

      ! If commands above output the message “docker command not found”, check that Docker Desktop is turned on and in its settings is checked a setting as on-screen below:
      Docker в WSL2
  • Now, inside the WSL2 we have the installed Docker, it remains to install Docker Compose
    https://docs.docker.com/compose/install/ .
    In our case, I highly recommend you consider the next installing options: Scenario two or Scenario three .
  • Then we have to add the user’s docker into the Linux subsystem:
sudo groupadd docker
sudo usermod -aG docker $USER
  • Close the terminal and open it again, then run a control check by following the instruction below:
docker run hello-world
# You should see `Hello from Docker!` in success result.    

# try to use one of them
docker-compose --version
docker compose --version

# You must see version higher 1.28

Viola, now our local environment based on WSL2 + Docker has been configured and is ready to work!

Performance

To get maximum performance using WSL2, it is recommended to store project files inside the WSL filesystem.

In case you put a working project in the Windows filesystem – you will feel a slump in performance.
This will be especially visible on large projects with a bloated vendor or node_modules directories.
This happens because Docker will access these files via a virtual network disc, that is connected to WSL.

Accessing to the WSL2 filesystem via PHPStorm and the file manager

Once you have placed the project files in the WSL, you can connect PHPStorm to the WSL project, at the path: \\wsl$\Ubuntu\home:

Docker with WSL2 based on Windows

As well you can get access to WSL by using the file manager of Windows at the paths \\wsl$\Ubuntu\home or \\wsl.localhost\Ubuntu\home:

To sum up

Now you can safely use WSL on Windows to bring up your projects in Docker containers. By storing your project files inside WSL2, you will no longer have to worry about performance issues.


Андрей Писаревский

Author: Andrei Pisarevskii 

PHP | WordPress Team Lead. I have commercial programming experience since 2010 and expertise in the full cycle of web development: Frontend, Backend, QA, Server administration, managing large teams and Enterprise projects.

Leave a Reply

Your email address will not be published. Required fields are marked *