Installing WSL 2
Open PowerShell as an administrator: Right-click the Windows Start button and select "Windows Terminal (Admin)" or "Command Prompt (Admin)."
Enable the WSL feature:
powershelldism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Enable the Virtual Machine Platform feature:
powershelldism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Restart your computer to complete the installation of the Virtual Machine Platform feature.
After restarting, open PowerShell as an administrator again.
Set WSL 2 as the default version:
powershellwsl --set-default-version 2
Install a Linux distribution of your choice from the Microsoft Store or by using the command-line. For example, to install Ubuntu:
powershellwsl --install -d Ubuntu
Initialize your newly installed Linux distribution and follow the prompts to set up a user and password.
WSL 2 is now installed and ready for use.
Installing Docker in WSL 2
To install Docker in WSL 2, follow these steps:
Open a WSL 2 terminal.
Run the following command:
bashsudo apt update && sudo apt install docker.io
Running the Docker Image
To run a Docker image in WSL 2, follow these steps:
Open a WSL 2 terminal.
Run the following command to start a Docker container based on an image:
bashdocker run -it <image_name>
For example, to run the hello-world image:
bashdocker run -it hello-world
This will start a new container and display the message "Hello from Docker!" on the console.
If you need to map specific ports, use the -p option. For instance, to run the hello-world image on port 80:
bashdocker run -it -p 80:80 hello-world
You can then access the container on port 80 of your machine.
Example
To run the nginx image in a Windows 10 VM with virtualization and Hyper-V enabled:
Install WSL 2.
Install Docker in WSL 2.
Run the nginx image with the following command:
bashdocker run -it -p 80:80 nginx
You can access the Nginx web server on port 80 of your host machine.
Note: If you are running Docker in a VM, you may need to configure port forwarding to access the container from the host machine.