Build a dual-system Docker environment based on Docker Desktop and WSL2

illustrate

Docker Desktop : It can be understood as the desktop version of Docker, or the Docker service running on Windows systems.
WSL2 : It can be understood as a tool on Windows. Through WSL2, the Linux subsystem can be run on Windows.

The dual-system Docker environment built through Docker Desktop and WSL2 can run two types of containers, Windows and Linux, on a Windows server at the same time. At the same time, the physical resources on the server can be directly called in the Linux system and container, such as: Physical graphics card.
The dual-system Docker environment built in this way saves resources and does not require the use of virtualization software to install a Linux virtual machine.
WSL2 is also a subsystem that runs through virtualization, but it uses the virtual machine tool that comes with Windows.
WSL1 and WSL2 are the two current versions of WSL, and their comparison is as follows (note that WSL 2 is only available in Windows 11 or Windows 10 version 1903, build 18362 or later).

Insert image description here

environment

Necessary environment:
1. It must be a physical machine with virtualization turned on;
2. The installed Windows system must support opening Hyper-v, Linux subsystem for Windows, and virtual machine platform functions;
My installation environment: The internal version of the operating system
on Huawei laptops installed with Windows 10 Professional is 19045.2965 and the Linux system is CentOS 7.9 19011.

Install

Start WSL2 service

1. Open Control Panel > Programs > Programs and Features > Start and Install Windows Features, check Hyper-V, Linux Subsystem for Windows, and Virtual Machine Platform, and then restart the computer as prompted.
img

2. After restarting, open PowerShell and execute the following command to switch to WSL2

wsl --set-default-version 2

Successful return is as follows

有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
操作成功完成。

Install CentOS system

1. CentOS7 is not officially provided. You can download it from the warehouse below. Select the version you want to download, or you can use other CentOS versions.

Just download "CentOS7.zip".
Insert image description here
2. After the download is completed, decompress the two files. Pay attention to which directory you put it in at this time, and the ext4.vhdx virtual disk will be created in the corresponding directory. It is not recommended to put it on the C drive. This file will become very large as time goes by. big.

  • CentOS7.exe
  • rootfs.tar.gz

Insert image description here
3. Double-click CentOS7.exeto install. An error may be reported during the first installation. You need to upgrade the kernel first, wsl_update_x64.msijust download the file and install it.

Insert image description here

4. Open PowerShell and execute the following command to view the CentOS status.

  • Normally it can only go to CentOS7. Here is the result after installing docker-desktop.

Insert image description here

  • Execute wsl to enter the CentOS7 system. The default is to enter CentOS7. Use wsl -d to switch to the system you need to enter.
    Insert image description here

Install Docker Desktop

1. Let’s first go to the official website to download the installation package.

  • https://www.docker.com/products/docker-desktop/

Insert image description here

2. Start the installation. Please pay attention to the following steps:
Insert image description here

Insert image description here

3. Wait for the installation to complete, open docker-desktop, and enable the CentOS7 system.
Insert image description here

4. Check through the wsl -l -v command. There will be three subsystems running.

  • CentOS7 Linux system services
  • docker-desktop-data docker-desktop data service
  • docker-desktop docker-desktop service
    Insert image description here

5. Enter CentOS7 and execute docker version. You can see that CentOS7 has installed the docker service and is consistent with the docker-desktop version.
Insert image description here

6. At this point, the dual-system docker environment has been installed. You can directly enter Linux to run the container, or run the container through docker-desktop.
File transfer between systems can be directly copied, and the ports opened by the container will also be mapped to the physical machine.
Insert image description here
Insert image description here

other

Install other distributions

Use the following command to view the online distribution version

wsl --list --online
以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

For example, to install Ubuntu-22.04, just use the following command, and the corresponding distribution will be automatically downloaded and installed.

wsl --install -d Ubuntu-22.04
正在安装: Ubuntu 22.04 LTS
[==========                18.0%

The disadvantage of this installation is that it can only be installed on the C drive. If you need to migrate, you can refer to the instructions below. After the installation is complete, you can see that there are two distributions.

wsl -l
适用于 Linux 的 Windows 子系统分发版:
CentOS7 (默认)
Ubuntu-22.04

After the installation is complete, you cannot use the root user. You will be prompted that the user already exists. You can use the following command to manually adjust it to root. Pay attention to removing special symbols. For example, after removing Ubuntu-22.04, it becomes Ubuntu2204.

Ubuntu2204 config --default-user root

The same command is used to enter the non-default new version. For example, in Ubuntu2204, press Enter to enter the terminal.

WSL command usage

# 启动
wsl

# 关闭wsl
wsl --shutdown

# 查看状态
wsl -l -v

# 注销
wsl --unregister CentOS7

Docker-Desktop storage path change

Note: By default, the two WSL subsystems initialized by Docker-desktop will be saved under the C drive to avoid occupying the C drive space and the storage path needs to be changed.

  • docker-desktop: saves the program
  • docker-desktop-data: saved image

Perform the operation with docker-desktop closed.

Migrate these two subsystems through wsl command

  • backup command
wsl --export docker-desktop docker-desktop.tar
wsl --export docker-desktop-data docker-desktop-data.tar
  • Delete command
wsl --unregister docker-desktop
wsl --unregister docker-desktop-data
  • import command
wsl --import docker-desktop D:\docker\docker-desktop docker-desktop.tar
wsl --import docker-desktop-data D:\docker\docker-desktop-data docker-desktop-data.tar

Note: The directories used by the two subsystem files cannot be the same directory. After completing the above operations, start docker-desktop to download the image file and it will not be saved to the C drive.

Guess you like

Origin blog.csdn.net/weixin_41166785/article/details/130989061