WSl2 and Dockers
A simple tutorial for setting up the Docker system on Windows 10
Step 0: Checking System Compatibility
Running Windows 10 doesn’t mean that you can use the Version 2 of Windows Subsystem for Linux. So before going forward, make sure that your computer has virtualization capabilities and you are running a suitable version of Windows.
Step 0.1: Checking Current Windows Version
For checking your OS Build you must open the Run Command Box using the keyboard shortcut Windows + R
, then type winver
and press Enter
.
The supported versions are Version 1903 or higher, with Build 18362 or higher.
Step 1: Install Windows Subsystem for Linux
Open the PowerShell with administrator privileges and run the following two commands.
Step 1.1: Enable Windows Subsystem for Linux
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Step 1.2: Enable Virtual Machine feature
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
After running this two commands, you will need to RESTART your machine.
Step 1.3: Set WSL2 as default
Now you have WSL on your machine, but you want to use Version 2, so for that to happen you must install the fowling package with the Linux Kernel.
Once the download is complete and the Kernel is installed, you will need to open one last time the PowerShell as administrator privileges and run the following command.
wsl --set-default-version 2
Step 1.4: Install your preferred Linux Distro
Now that we are using the WSL2 by default, we must install a Linux Distro. In this tutorial we will use Ubuntu 20.04 LTS.
When you open Linux Distro for the first time, you need to enter your username and password.
After this the WSL2 installation is complete, but let’s make sure the default distro is the one we just installed. To do this, open PowerShell as administrator and run the following command (Change Ubuntu-20.04 to your chosen distro).
wsl --set-default Ubuntu-20.04
The next sub-steps are optional, they are just personal preferences if you don’t want to do that skip to Step 2.
Step 1.5: Bind Windows hosts file with WSL2
For this we need to edit some permissions in the Windows hosts file, so please be aware that it can be risky and I am not responsible for any damages.
If you want to continue, you only need to open the File Explorer
and go to the location C:\Windows\System32\drivers\etc\
.
Now you need to Right-click
the hosts file and select Properties from the context menu.
Go to the Security tab and Click
Edit under the Groups or user names.
In the next window Click
Add and it will open a new window called Select Users or Groups.
In the box enter your username, Click
Check Names and then Click
OK.
The user will now appear in the Permissions for hosts window, however we must ensure that our user has the permissions as in the following image and then Click
Apply.
To finish the binding open the Linux Distro and run the next two commands.
sudo bash -c "printf '[network]\ngenerateHosts = false\n' > /etc/wsl.conf"sudo ln -sf /mnt/c/Windows/System32/drivers/etc/hosts /etc/hosts
Step 1.6: Set a static IP to WSL2
The WSL2 always reset all IP configurations on reboot, which is not a big problem. But if you want to use tools like XDebug in Docker, then you need a fixed IP.
For that you need to open the Task Scheduler and Click
Create Task… on the right side. Go to Triggers tab, Click
New… and select the same options as the next image.
Go to the Actions tab, Click
New… and define the following settings in the corresponding boxes (Change Ubuntu-20.04 to your chosen distro).
Program/script:
C:\Windows\System32\cmd.exe
Add arguments:
/c "wsl.exe -d Ubuntu-20.04 -e sudo /usr/local/bin/wsl2-ip && wsl.exe -t Ubuntu-20.04"
Start in:
C:\Windows\System32
Before adding the task, make sure that your General tab and your Conditions tab looks like the following images.
Now go to your chosen Distro, run sudo nano /usr/local/bin/wsl2-ip
and paste the next code.
#!/usr/bin/env bash{
ip addr add 192.168.111.2/24 broadcast 192.168.111.255 dev eth0 label eth0:1
netsh.exe interface ipv4 add address "vEthernet (WSL)" 192.168.111.1 255.255.255.0
} &> /dev/null
exit 0;
Then we will put the script to run automatically through the following command.
sudo bash -c 'sudo chmod 755 /usr/local/bin/wsl2-ip && printf "\nsudo /usr/local/bin/wsl2-ip\n" >> /etc/profile'
And to finish we just need to disable the password prompt when running the script. To do so, simply run the next command.
sudo bash -c 'echo "%sudo ALL=(ALL) NOPASSWD:/usr/local/bin/wsl2-ip" >> /etc/sudoers'
Now you add a variable to .bashrc for use that . To do so, simply run the next command.
echo 'export XDEBUG_HOST="192.168.111.2"' >> ~/.bashrc
This will be usefull if you want to use Now you add a variable to .bashrc for use that . To do so, simply run the next command.
environment:
XDEBUG_CONFIG: "client_host=${XDEBUG_HOST:-host.docker.internal}"
Step 2: Install Docker Desktop for Windows
Step 2.1: Download and install Docker Desktop
Installing docker on Windows is very simple now, we just need to run the installer from the link above and it’s done.
Step 2.2: Enable integration with WSL2
Now we have to ensure that the docker is using WSL2. For that, we have to open the Docker Desktop,Click
the Gear button at the top and check if the integration is active in the resources tab, as in the image below.
Thank you for reading this tutorial, don’t hesitate to comment your ideas and suggestions.