wsl中docker自启动

前言

wsl -l -v # 查看 wsl 的状态
wsl -t Ubuntu # 关闭指定版本
wsl -d Ubuntu # 启动指定版本

启动 docker

通过命令 ps -p 1 -o comm= 我们知道我们用的是 init,故我们将 systemd 命令修改为 SysV init命令。

ps -p 1 -o comm=
在这里插入图片描述

更换命令,用SysV init的命令代替systemd

$ sudo service docker start
$ service docker status

自启动 docker

WSL2内操作

sudo vim /etc/init.wsl
#! /bin/sh
service docker start
sudo chmod +x /etc/init.wsl

Windows内操作

  1. 在 windows 系统上创建一个启动脚本 startup.vbs
Set ws = WScript.CreateObject("WScript.Shell")        
ws.run "wsl -d ubuntu -u root /etc/init.wsl"

注:其中‘ubuntu’为WSL2子系统的名字

  1. 按 win+R 输入 shell:startup 将以上vbs文件放入该目录

说明

很多教程都说使用 sudo systemctl enable docker 和 sudo systemctl start docker,但是实际会报错。

用window10下WSL使用Ubuntu20.04,当我使用命令sudo systemctl status docker的时候报错:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

问题原因:
如果是一般的Linux操作系统,可能是因为Linux中没有使用systemd,想用systemd命令来管理Linux上的服务,因此会报错,很可能是使用的是经典的SysV init(sysvinit)系统。
但我这个是window10下WSL的Ubuntu,就会使SysV init而不是systemd。

解决方法:
更换命令,用SysV init的命令代替systemd的

image.png

reference

@article{glc4002021Sep,
author = {glc400},
title = { {WSL系统systemctl不能使用解决方法}},
journal = {SegmentFault 思否},
year = {2021},
month = sep,
urldate = {2023-06-20},
publisher = {SegmentFault},
language = {chinese},
url = {https://segmentfault.com/a/1190000040670856}
}
@misc{BibEntry2023Jun,
title = { {在Windows11中Linux子系统安装Docker}},
journal = {知乎专栏},
year = {2023},
month = jun,
urldate = {2023-06-20},
language = {chinese},
note = {[Online; accessed 20. Jun. 2023]},
url = {https://zhuanlan.zhihu.com/p/433898505}
}
@misc{BibEntry2023May,
title = { {wsl2 配置服务自启动 {\textendash} 梦回故里}},
year = {2023},
month = may,
urldate = {2023-06-20},
language = {chinese},
note = {[Online; accessed 20. Jun. 2023]},
url = {https://www.80shihua.com/archives/2890}
}

猜你喜欢

转载自blog.csdn.net/orDream/article/details/131311175