一、WSL导出子系统
首先查看存在的子系统
wsl -l -v
在powershell执行以下指令
wsl --export sy D:\WSL\sy_image.tar
sy是要打包的子系统名字,需要与上面查询的一致
D:\WSL\sy_image.tar为打包路径
二,WSL导入子系统
命令格式:wsl --import <DistributionName> <安装位置> <tar文件名> 导入时改变DistributionName(如Ubuntu-20.04改成Ubuntu),会导致WSL不可用。
wsl --import sy D:\WSL\sy D:\WSL\sy_image.tar
三 window 访问wsl 内docker服务
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8888 connectaddress=172.25.23.136 connectport=80
#例如:上面我是用wsl的ubuntu中的80端口映射到了容器的80端口,所以我这边的connectport要填80,172.25.23.136 是我ubuntu的eth0的ip
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3308 connectaddress=172.25.23.136 connectport=3308
四 wsl和本机共享统一网段
每次重启wsl ip会变化 绑定固定ip
wsl.bat
@echo on
setlocal enabledelayedexpansion
::wsl --shutdown
::wsl -u root service docker start | findstr "Starting Docker" > nul
::if !errorlevel! equ 0 (
:: echo docker start success
::)
:: set wsl2 ip
wsl -u root ip addr | findstr "192.168.169.2" > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
wsl -u root ip addr add 192.168.169.2/28 broadcast 192.168.169.15 dev eth0 label eth0:1
echo set wsl ip success: 192.168.169.2
)
:: set windows ip
ipconfig | findstr "192.168.169.1" > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
netsh interface ip add address "vEthernet (WSL)" 192.168.169.1 255.255.255.240
echo set windows ip success: 192.168.169.1
)
pause
::cmd