Github开源生信云平台 DEMO
没有。 Microsoft 只提供空的 Ubuntu 发行版(wsl --install -d Ubuntu),不提供预装 dockerd 的版本。Docker 官方提供的是 Docker Desktop(走 Docker Desktop 自己的 WSL 发行版 docker-desktop,不是你自己的 Ubuntu)。
wsl --install -d Ubuntu
docker-desktop
所以要得到“Ubuntu + dockerd”有两条路:
你目标是“Ubuntu 里运行 dockerd”,就是路线 A。下面全是可在 PowerShell(管理员)里手动执行的命令。
wsl --status wsl --version wsl -l -v
wsl -l -v 输出示例:
wsl -l -v
NAME STATE VERSION * Ubuntu Running 2 docker-desktop Stopped 2
注意 STATE 列:Running / Stopped / Installing。
Running
Stopped
Installing
wsl --install -d Ubuntu --no-launch
--no-launch
wsl --set-default-version 2
首次启动并设置用户(如已 --no-launch,需要显式启动一次完成初始化):
wsl -d Ubuntu -u root -- sh -lc "echo initialized"
一条命令搞定(用 root,避免 sudo 密码问题):
wsl -d Ubuntu -u root -- bash -lc @' set -e export DEBIAN_FRONTEND=noninteractive apt-get update apt-get install -y ca-certificates curl gnupg iptables install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc chmod a+r /etc/apt/keyrings/docker.asc echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" > /etc/apt/sources.list.d/docker.list apt-get update apt-get install -y docker-ce docker-ce-cli containerd.io '@
PowerShell 里 @'...'@ 是 here-string,可安全包含 $ 和多行。千万别用双引号包裹,否则 $(...) 会被 PowerShell 提前展开。
@'...'@
$
$(...)
关键点:WSL Ubuntu 默认不一定启用 systemd(旧版没有)。先确认:
wsl -d Ubuntu -u root -- bash -lc "ps -p 1 -o comm=; systemctl is-system-running 2>/dev/null || echo no-systemd"
systemd
running/degraded
init
no-systemd
写 daemon.json(同时保留 unix socket 和 tcp):
daemon.json
wsl -d Ubuntu -u root -- bash -lc @' mkdir -p /etc/docker cat > /etc/docker/daemon.json <<"EOF" { "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] } EOF mkdir -p /etc/systemd/system/docker.service.d cat > /etc/systemd/system/docker.service.d/override.conf <<"EOF" [Service] ExecStart= ExecStart=/usr/bin/dockerd EOF systemctl daemon-reload systemctl enable docker systemctl restart docker '@
为什么必须加 override.conf:Ubuntu 的 docker.service 默认带 -H fd://,而 daemon.json 里一旦出现 hosts 就会与命令行参数冲突报错 can't create unix socket ... hosts and -H cannot be combined。清空 ExecStart 再重写,才让 daemon.json 生效。
override.conf
-H fd://
hosts
can't create unix socket ... hosts and -H cannot be combined
ExecStart
手动前台起(先测能不能通):
wsl -d Ubuntu -u root -- bash -lc @' mkdir -p /etc/docker cat > /etc/docker/daemon.json <<"EOF" { "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] } EOF nohup dockerd > /var/log/dockerd.log 2>&1 & sleep 3 tail -n 20 /var/log/dockerd.log '@
注意:这条命令结束后,WSL 实例若没有其他常驻进程,几秒后会休眠,dockerd 也随之被杀。手动测试时没关系,但要长期保持,得让上一条命令保持不返回,或另开一个 wsl 会话挂着。
wsl
# 进程 wsl -d Ubuntu -u root -- bash -lc "pgrep -x dockerd && echo PROCESS_OK" # TCP 监听 wsl -d Ubuntu -u root -- bash -lc "ss -tln | grep 2375" # 本地 docker 可用 wsl -d Ubuntu -u root -- bash -lc "docker version --format '{{.Server.Version}}'" # 取 WSL IP wsl -d Ubuntu -u root -- bash -lc "hostname -I | awk '{print \$1}'"
最后一条的输出(如 172.24.123.45)就是 NAT 模式下 Windows 要连的地址。
172.24.123.45
先确认网络模式:
Get-Content "$env:USERPROFILE\.wslconfig" -ErrorAction SilentlyContinue
networkingMode=mirrored
hostname -I
$ip = (wsl -d Ubuntu -u root -- bash -lc "hostname -I | awk '{print `$1}'").Trim() Test-NetConnection -ComputerName $ip -Port 2375
Test-NetConnection -ComputerName 127.0.0.1 -Port 2375
通了之后再试 Docker API:
$env:DOCKER_HOST = "tcp://<上面的可用地址>:2375" docker version # 有 Server 段即成功
若本机没装 docker CLI,用 PowerShell 直接打 API:
Invoke-RestMethod -Uri "http://<地址>:2375/version"
hosts and -H cannot be combined
Cannot connect to the Docker daemon at unix:///var/run/docker.sock
Test-NetConnection
ss
iptables: No chain/target/match by that name
apt-get install -y iptables
dockerd
dockerd --validate --config-file /etc/docker/daemon.json
wsl -l -q
# 只停实例(保留数据) wsl --terminate Ubuntu # 彻底卸载发行版 wsl --unregister Ubuntu
建议按这个顺序手动跑一遍:阶段1 → 2 → 3 → 4(先路线1,不行再路线2)→ 5 → 6。等你在自己机器上确认哪条路线和哪个地址可用,我再把这些命令按 Inspect() + Ensure() 的结构落成 Go 代码。
Inspect()
Ensure()