Docker starts automatically in wsl

Preface

wsl -l -v# Check wslthe status of
wsl -t Ubuntu# Close the specified version
wsl -d Ubuntu# Start the specified version

start updocker

Through the command, ps -p 1 -o comm=we know that we are using it init, so we modify the systemd command to the SysV init command.

ps -p 1 -o comm=
Insert image description here

Replace the command and use the SysV init command instead of systemd

$ sudo service docker start
$ service docker status

self startdocker

Operation within WSL2

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

Operation within Windows

  1. Create a startup script on Windows systems startup.vbs.
Set ws = WScript.CreateObject("WScript.Shell")        
ws.run "wsl -d ubuntu -u root /etc/init.wsl"

Note: 'ubuntu' is the name of the WSL2 subsystem

  1. Press win+R and enter shell:startup and put the above vbs file into this directory.

illustrate

Many tutorials say to use sudo systemctl enable docker and sudo systemctl start docker, but in fact, errors will be reported.

I use Ubuntu20.04 under WSL under Windows 10. When I use the command, I sudo systemctl status dockerget an error:

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

Cause of the problem:
If it is a general Linux operating system, it may be because systemd is not used in Linux and you want to use the systemd command to manage services on Linux, so an error will be reported. It is likely that the classic SysV init (sysvinit) system is used.
But mine is Ubuntu with WSL under window 10, so it will use SysV init instead of systemd.

Solution:
Replace the command and use the SysV init command instead of systemd.

image.png

reference

@article{glc4002021Sep,
author = {glc400},
title = { {{Solution to WSL system systemctl cannot be used}},
journal = {SegmentFault Sifu},
year = {2021},
month = sep,
urldate = {2023-06- 20},
publisher = {SegmentFault},
language = {chinese},
url = {https://segmentfault.com/a/1190000040670856}
}
@misc{BibEntry2023Jun,
title = { {Installing Docker on the Linux subsystem in Windows 11}} ,
journal = {Zhihu column},
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 configuration service self-starting{\textendash} Dreaming back to my hometown}},
year = {2023},
month = may,
urldate = {2023-06-20},
language = {chinese},
note = {[Online ; accessed 20. Jun. 2023]},
url = {https://www.80shihua.com/archives/2890}
}

Guess you like

Origin blog.csdn.net/orDream/article/details/131311175