WSL2划分虚拟局域网并启动docker脚本

Scroll Down

WSL2划分虚拟局域网并启动docker脚本

  1. 将下面脚本保存为 WSL2划分虚拟局域网并启动docker脚本.bat
  2. 将该bat脚本复制到 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup目录下
  3. 重启系统。
@echo off
setlocal enabledelayedexpansion

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