使用wsl2 在windows 下安装 Docker

系统要求

建议先升级windows10到20H1/20H2 or higher, 预设就会是wsl 2 版本

启用安装wsl

wsl2 需要使用Hyper-V,请确认已经打开Hyper-V 的功能

控制面板 --> 程序和功能 --> 启用或关闭 Windows 功能 --> 勾选 Hyper-V

8ce216794509d5de0a320c2db1bdbaa.png

家庭版没有Hyper-V,先新建一个文本文件,复制粘贴一下代码到文本中,保存为Hyper-V.cmd,使用管理员运行,等

重新启动完成就可以看见Hyper-V了

pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL

以管理员身份执行以下 PowerShell 命令

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重启电脑,然后到 控制面板 --> 程序和功能 --> 启用或关闭 Windows 功能 --> 勾选 适用于 Linux 的 Windows 子系统和虚拟机平台

4df80c6d036662cf09731059160df7a.png

WSL 版本改成WSL2

在 PowerShell 中执行以下命令

wsl --set-default-version 2

安裝 Linux 发行版本

在 windows 10 中启动 Microsoft Store,然后在商店中搜索ubuntu,这边使用是Ubuntu 22.04.3 LTS

4db2d65761b2600d6ea661c233041da.png

安装后启动,设置好账号密码

更换Systemd

很多linux 应用都需要systemd 来管理和启动服务,WSL2 是windows 下的一个VM,微软制作了一个init(PID 1),快速启动WSL VM 以及与windows 通信,执行 systemctl 确认是否有 systemd

ubuntu@DESKTOP:/mnt/$ systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

GitHub 上已经有构建systemd(PID 1)环镜,这里我们使用DamionGans/ubuntu-wsl2-systemd-script,按照说明操作就可以了

ubuntu@CM-PC-02057:~$ git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
Cloning into 'ubuntu-wsl2-systemd-script'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 118 (delta 0), reused 2 (delta 0), pack-reused 115
Receiving objects: 100% (118/118), 34.22 KiB | 455.00 KiB/s, done.
Resolving deltas: 100% (57/57), done.
ubuntu@CM-PC-02057:~$ cd ubuntu-wsl2-systemd-script
ubuntu@CM-PC-02057:~/ubuntu-wsl2-systemd-script$ bash ubuntu-wsl2-systemd-script.sh

安装成功后退出,重启ubuntu,使用 wsl -t Ubuntu,Ubuntu 换成你的ubuntu 的名字,使用wsl -l 查看,这里是Ubuntu-20.04,所以执行的是 wsl -t Ubuntu-20.04

ubuntu@CM-PC-02057:~/ubuntu-wsl2-systemd-script$ exit
logout

在 PowerShell 中执行以下命令

PS C:\WINDOWS\system32> wsl -l
适用于 Linux 的 Windows 子系统分发:
Ubuntu-20.04 (默认)

PS C:\WINDOWS\system32> wsl -t Ubuntu-20.04
PS C:\WINDOWS\system32> wsl -d Ubuntu-20.04

检查有没有成功启动 systemd

ubuntu@DESKTOP:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   2452  1608 hvc0     Sl+  09:57   0:00 /init
root         6  0.0  0.0   2476   132 hvc0     Sl+  09:57   0:00 plan9 --control-socket 5 --log-level 4 --server-fd 6 --
root        10  0.0  0.0   2472   116 ?        S    09:57   0:00 /init
root        44  0.0  0.0   7240   516 ?        Ss   09:58   0:00 /usr/bin/unshare --fork --pid --mount-proc bash -c expo
root        46  0.0  0.3  26864 12080 ?        Ss   09:58   0:00 /lib/systemd/systemd --unit=basic.target
root        88  0.0  0.3  51672 15500 ?        S<s  09:58   0:00 /lib/systemd/systemd-journald
root       105  0.0  0.1  21596  7100 ?        Ss   09:58   0:00 /lib/systemd/systemd-udevd
systemd+   117  0.0  0.2  18552  7928 ?        Ss   09:58   0:00 /lib/systemd/systemd-networkd
message+   254  0.0  0.1   7364  4024 ?        Ss   09:58   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofo
root       334  0.0  0.1  16940  7640 ?        Ss   09:58   0:00 /lib/systemd/systemd-logind
root       660  0.0  0.0   2460   816 ?        Ss   10:13   0:00 /init
root       661  0.0  0.0   2472   116 ?        S    10:13   0:00 /init
root       662  0.0  0.0   2616   596 pts/0    Ss+  10:13   0:00 sh
root       671  0.0  0.0   2476   120 ?        S    10:13   0:00 /init
root       672  0.0  0.6 1247876 24184 pts/1   Ssl+ 10:13   0:00 /mnt/wsl/docker-desktop/docker-desktop-user-distro prox
root       688  0.0  0.0   2476   120 ?        S    10:13   0:00 /init
root       689  0.0  1.0 768592 42792 pts/2    Ssl+ 10:13   0:00 docker serve --address unix:///root/.docker/run/docker-
root       723  0.0  0.0   2456   112 ?        Ss   10:51   0:00 /init
root       725  0.0  0.0   2472   116 ?        R    10:51   0:00 /init
root       728  0.0  0.1  10048  5148 pts/3    Ss   10:51   0:00 -bash
root       766  0.0  0.0  10608  3220 pts/3    R+   11:11   0:00 ps aux

安装Docker

有了systemd,就可以安装 docker engine 了,按照官方下载安装就可以了

ubuntu@DESKTOP:~# docker --version
Docker version 24.0.7, build afdd53b

可能会遇到的问题

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容