Install the WSL

1. Open the WSL

The Optional Windows Subsystem for Linux feature must be enabled and restarted before you can run the Linux distribution on Windows.

Run Powershell as administrator (enable WSL, skip if enabled)

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux Enable-WindowsOptionalFeature - online-featurename VirtualMachinePlatform - enable the VirtualMachinePlatform optional componentCopy the code

2. Install WSL Linux distribution

Compare WSL 1 and WSL 2

The main differences between WSL 1 and WSL 2 are the use of the actual Linux kernel within the managed VM, support for full system call compatibility, and performance across Linux and Windows operating systems.

WSL2 supports Docker perfectly compared to WSL1. Unlike WSL1’s emulated Linux API, WSL2 uses a scheme that runs in a Hyper-V virtual machine. We can say that WSL2 is very close to the original Linux

  • WSL1 does not have a Linux kernel and does not support Docker. WSL2 is a lightweight virtual machine with Linux kernel and Docker support.
  • At present, Docker has been adapted for WSL2. When WSL2 is detected during installation, the WSL2 backend will be enabled. Docker server can be operated under Windows and WSL at the same time. It also starts faster and consumes less resources than previous Versions of Docker on Windows.

WSL1

Docker daemons are not supported, but you can use the Docker CLI to connect to remote Docker daemons running through Docker for Windows or any other VM you create

Not fully compatible with systemctl, systemd, etc., IO speed relative to native Linux is also greatly reduced, in the compilation and decompression aspects can be deeply experienced. Not really Linux, after all, but a pseudo-Linux system that hangs on top of a Windows NT kernel

WSL1 is integrated with Docker

Since WSL is not yet a native Linux, it is neededDocker for WindowsTo implement Docker (Docker for Windows is based on Hyper-V technology)

Another way is to install docker client in WSL1 and connect to Docker Server to solve the problem of using Docker on WSL1. (PS: Win10 Linux subsystem is not installed docker (server), but can install the client)

sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common 
curl -fsSL [https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg](https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg) | sudo apt-key add - 
sudo apt remove gpg 
sudo apt install gnupg1 
sudo apt install software-properties-common
sudo add-apt-repository "deb [arch=amd64] [https://mirrors.aliyun.com/docker-ce/linux/ubuntu](https://mirrors.aliyun.com/docker-ce/linux/ubuntu) $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get -y install docker-ce
echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
Copy the code

reference

  • Running Docker containers on Bash on Windows from the < blog.jayway.com/2017/04/19/… >
  • Setting Up Docker for Windows and WSL to Work Flawlessly from < nickjanetakis.com/blog/settin… >

WSL2

WSL2 is the second generation WSL that was included in the May 2020 official release of Windows 10 V2004. Compared to the first generation, the new WSL2 has a redesigned architecture and uses a true Linux kernel with almost all the full functionality of Linux. Wsl2-enabled Linux systems start up quickly and use very little memory. WSL2 can also run Docker, VS Code editor and remote-WSL plug-in natively. A full Linux VM does not support systemctl and systemd, and cannot run graphical desktops directly. Windows is also becoming more and more virtual platform, Windows NT kernel and Linux kernel are running on virtual platform, is horizontal

We will replace the Hyper-V VM we currently use by a WSL 2 integration package. This package will provide the same features as the current Docker Desktop VM: Kubernetes 1-click setup, automatic updates, transparent HTTP proxy configuration, access to the daemon from Windows, transparent bind mounts of Windows files, and more.

Upgrade to WSL2

If VERSION 1 is found, Ubuntu is running on WSL1 and can be upgraded to WSL2. Similarly, WSL2 can be demoted to WSL1. WSL -l -v # check the installed Linux version and name. WSL –list –verbose**WSL2 requires the Windows Build 18917 update, **WSL does not. But it is better to update the operating system to the required version first, rather than installing WSL first and then updating WSL 2, which requires updating its kernel components. For information, visitaka.ms/wsl2kernel

For details, see docs.microsoft.com/zh-cn/windo…

Before installing WSL 2, you must enable the optional vm Platform functionWSL –set-version Ubuntu 2 # Upgrade to WSL2, where Ubuntu is the name of the installed Linux, command format: WSL –set-version

With WSL enabled, WSL files can be accessed via \ WSL $

WSL2 is integrated with Docker

The original WSL was designed to emulate the Linux kernel on top of Windows, but there are such fundamental differences between Windows and Linux that certain things cannot be implemented with the same behavior as native Linux, meaning that it is impossible to run directly in WSLDocker EngineAnd Kubernetes in WSL1, Docker Desktop through hyper-V virtual machine to run the Docker environment

After upgrading to WSL2, Docker Desktop can run directly on the Linux kernel using WSL2

Docker Desktop integrates with the WSL2 distribution by setting the following Settings, without installing Docker in WSL. Of course, you can also choose not to integrate with Docker Desktop and run the Docker container directly in WSL.

After WSL is enabled, docker running data is in the WSL distribution, and file locations are managed only by WSL

After docker is installed, Docker will automatically create 2 distributions:

  • docker-desktop
  • docker-desktop-data

WSL distributions default are installed in the disk C, in % LOCALAPPDATA % / Docker/WSL Docker operation data and image file is % LOCALAPPDATA % / Docker WSL/data/corruption VHDX

You can still experience seamless integration with Windows through WSL 2 integration, but Linux programs running in WSL can do the same. This has a huge impact on developers working on projects for Linux environments or building processes tailored to Linux. No more Linux and Windows build scripts to maintain! For example, a Docker developer can now work on a Linux Docker daemon on Windows using the same set of tools and scripts as a developer on a Linux computer:

Note: The most cool part of WSL2 Docker is to share the network with the host Win10. We can access the services in the corresponding Container in the Docker by using localhost and port number in the host Win10. It is very convenient

WSL use

WSL # start WSL –shutdown # shutdown all running Linux and WSL 2

After WSL2 is started, an additional virtual machine worker thread is found in task manager

Access WSL files

Uninstalling a Linux distribution Sometimes a Linux distribution is no longer in use, or the environment is corrupted and needs to be reinstalled. Here’s how.

  • First use the: wslconfig /l command to view the Linux distribution installed on your system.
  • Then unmount the specified distribution of Linux using: wslconfig /u < distribution name >.