前提
1、win10以上系统
2、已有docker镜像备份
一、安装WSL2
1、开启wsl
进入控制面板→程序和功能→启用或关闭Windows功能,开启「Hyper-V」、「适用于Linux的Windows子系统」、「虚拟机平台」三项

开启后需要重启电脑
2、升级wsl2
开启后默认为wsl,我们需要升级为wsl2。
首先我们要先更新wsl,这个尤为重要,否则后续无法使用systemctl命令
输入“wsl --update”命令以更新WSL内核
然后我们根据文档,手动安装一下内核包
官方文档链接 https://learn.microsoft.com/zh-cn/windows/wsl/install#upgrade-version-from-wsl-1-to-wsl-2
点击「安装内核包」

跳转升级说明页面,再次点击「适用于 x64 计算机的 WSL2 Linux 内核更新包」进行下载升级包

升级包安装链接 https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
下载后安装即可
3、设置默认WSL2版本
以管理员身份打开PowerShell,将WSL2设置为默认版本,后续安装的Linux子系统就默认为WSL2了
wsl --set-default-version 2如果想只设置某个子系统为WSL2,或已安装了子系统,已经为WSL,想设置为WSL2,则执行以下命令
1)、查询当前子系统列表
wsl -l -v2)、设置为WSL2
wsl --set-version 「系统名称」 2注:如果还未安装Linux子系统,可到应用商店用安装
操作完成最好是重启WSL2或重启系统,重启WSL2方法 https://www.itfuncn.com/blog/548/
二、安装Docker
为了方便使用,我们使用阿里云镜像源安装docker
1、安装依赖包
apt-get -y install apt-transport-https ca-certificates curl gnupg lsb-release2、添加阿里云镜像源和密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg3、添加阿里云镜像源
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null4、更新安装工具包
apt-get update5、安装Docker套件
apt-get -y install docker-ce docker-ce-cli containerd.io注:由于我们使用的是本地镜像,所以不再配置docker镜像源,需要用到远程镜像的,开源自行配置docker国内镜像源
6、查看docker是否安装成功
docker images如果显示有误,可尝试重启守护进程和docker引擎
systemctl daemon-reload
systemctl restart docker如果该命令提示「System has not been booted with systemd as init system (PID 1). Can‘t operate.」,首先重启WSL2或重启系统,然后再次尝试,原因是可能由于刚刚升级WSL2,系统未应用,WSL不支持systemctl命令。
如果还未解决,再尝试到子系统中,编辑/etc/wsl.conf文件:
[boot]
systemd=true然后重启WSL2
文档说明 https://learn.microsoft.com/en-in/windows/wsl/wsl-config#configuration-settings-for-wslconf
其他说明
操作过程中如果有错误提示:Job for docker.service failed because the control process exited with error code.See “systemctl status docker.service” and “journalctl -xeu docker.service” for details.则可依次执行以下命令
update-alternatives --set iptables /usr/sbin/iptables-legacy
apt reinstall docker-ce
rm -f /etc/docker/daemon.json
systemctl start docker
systemctl daemon-reload
systemctl restart docker三、导入本地镜像
docker load < 「镜像压缩包」更详细的镜像导入和导出,可参考 https://www.itfuncn.com/blog/562/
查看镜像列表
docker images