Wsl2安装CentOS7

    |     2023年9月10日   |   web前端技术   |     0 条评论   |    533

WSL(Windows Subsystem for Linux)让你在 Windows 里跑 Linux。 本篇把 Docker 里的 CentOS 7 镜像导出成 tar,再用 wsl --import 装成 WSL2 发行版。前提:本机已能跑虚拟化,并且 Docker 已经装好。

同一系列的后续篇:Docker 简介和安装、Docker 快速安装软件、制作自己的镜像、Docker-Compose发布与部署。

Snipaste_2023-10-10_11-33-14

一、从 DockerHub 拉 CentOS 7

先到 CentOS 的 DockerHub 标签页 确认要的版本,再拉取。若本地已经有该镜像,状态会显示 Image is up to date:

[root@localhost128 ~]# docker pull centos:centos7
centos7: Pulling from library/centos
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Image is up to date for centos:centos7
docker.io/library/centos:centos7
[root@localhost128 ~]# docker images
REPOSITORY               TAG             IMAGE ID       CREATED         SIZE
centos                   centos7         8652b9f0cb4c   9 months ago    204MB

二、跑起来再导出 tar

docker run centos:centos7 默认执行 /bin/bash 后会立刻退出,这是正常现象。用 docker ps -a 拿到容器 ID,再 docker export:

[root@localhost128 ~]# docker run centos:centos7
[root@localhost128 ~]# docker ps -a
CONTAINER ID   IMAGE            COMMAND       CREATED         STATUS
44604d80ea3a   centos:centos7   "/bin/bash"   9 seconds ago   Exited (0) 7 seconds ago
[root@localhost128 ~]# docker export 44604d80ea3a >  centos7.tar
[root@localhost128 ~]# ls
anaconda-ks.cfg  centos7.tar

把 tar 下载到 Windows 本机,例如 D:\Linux\centos7.tar:

三、wsl –import 装进 WSL2

命令文档见 Wsl2 命令文档。三个参数分别是发行版名字、安装目录、tar 路径:

wsl --import  centos7  D:\Linux\CentOS7   D:\Linux\centos7.tar

四、设为默认并发射版管理

wsl -l 列出发行版,wsl --set-default centos7 把它设为默认。再用 wsl -l -v 确认 VERSION 为 2:

PS C:\WINDOWS\system32> wsl -l
适用于 Linux 的 Windows 子系统分发版:
docker-desktop (默认)
centos7
docker-desktop-data

命令: wsl --set-default <Distribution Name>
$>wsl --set-default centos7

PS C:\WINDOWS\system32> wsl -l -v
  NAME                   STATE           VERSION
* centos7                Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

终止某个发行版:wsl --terminate <Distribution Name>。把 docker-desktop 停掉后,只留 centos7 在跑:

PS C:\Users\HUAWEI> wsl --terminate docker-desktop
PS C:\Users\HUAWEI> wsl --terminate docker-desktop-data
PS C:\Users\HUAWEI> wsl -l -v
  NAME                  STATE           VERSION
* centos7                Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
步骤 命令 得到什么
拉镜像 docker pull centos:centos7 本地 centos:centos7
跑一次并列出 docker run / docker ps -a 容器 ID
导出 docker export ID > centos7.tar 可导入的根文件系统
导入 WSL wsl --import centos7 目录 tar 名为 centos7 的发行版
默认 / 停止 wsl --set-default / wsl --terminate 日常管理

一句话总结:WSL2 安装 CentOS 7 的捷径:Docker 拉镜像 → export 成 tar → wsl –import,再 set-default 即可当日常发行版用。

转载请注明来源:Wsl2安装CentOS7
本文链接地址:https://ai.zhousir.top/?p=3430
回复 取消