"sudo systemctl enable docker"不可用:在WSL2上启动时自动运行Docker(使用"sysvinit"/"init"命令或解决方法) [英] "sudo systemctl enable docker" not available: Automatically run Docker at boot on WSL2 (using a "sysvinit" / "init" command or a workaround)

查看:215
本文介绍了"sudo systemctl enable docker"不可用:在WSL2上启动时自动运行Docker(使用"sysvinit"/"init"命令或解决方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WSL2上使用Ubuntu.

根据

我可以选择 service docker start 来运行docker,它可以工作.但是我找不到像"systemd"的 sudo systemctl enable docker 这样的"sysvinit"之类的东西.我希望它像:

  sudo服务docker enable 

但是,启用"不适用于"sysvinit"/"init".

虽然 sudo服务docker start 的工作方式与 sudo systemctl start docker 一样,但是没有这样的命令使用"enable".此刻,每当我启动WSL2时,我都需要运行 sudo服务docker start .

问题:

使用 sudo服务docker ... 到达 sudo systemctl enable docker 的命令是什么,或者如果该命令不存在,那么这里的解决方法是什么在WSL2上打开Ubuntu时自动启动docker?

解决方案

对""的简短回答是什么?在WSL2上打开Ubuntu时,自动启动docker的解决方法是什么?

  • 在您的 user 启动脚本(例如 .profile )中运行必要的命令.进行检查以查看服务是否先运行,例如:

    sudo服务docker状态||sudo服务docker start .

  • visudo 或向/etc/sudoers.d 中添加规则,以使您的用户无需密码即可运行命令:

    用户名ALL =(root)NOPASSWD:/usr/sbin/service docker *

更多详细信息:

您已经发现,WSL不包括任何systemd支持,也没有任何有关在启动时启动服务的直接支持.

对于初学者来说,WSL子系统不会在Windows启动时启动,而只会在用户启动登录会话时启动.因此,如果没有任何实际的系统启动",init.d或systemd启动就没有太大意义.

此外,用户可能正在运行多个WSL实例/分发,如果您这样做(如我所愿),那么您真的不希望 all all 个服务em>实例在每次启动时运行.

但是,对于Docker,您是在运行具有WSL2集成的Docker Desktop,还是直接安装在WSl2实例中?对于Docker桌面,我昨天遇到了问题,这是另一个有关如何在Windows启动时启动Docker桌面守护程序的问题.

您还可以在用户登录时通过Windows Task Manager启动WSL2实例,并通过Task Manager中的 wsl sudo service docker start 之类的脚本运行脚本.

请注意,在Windows boot 上似乎没有作用,(仅 login )是因为Windows似乎终止了所有未绑定的WSL实例几秒钟后(即使服务在后台运行)也向活跃用户显示.您可以使用PowerShell Invoke-WmiMethod 解决此问题,例如...

powershell.exe调用-WmiMethod -Class Win32_Process -Name创建-ArgumentList'wsl',尽管我还没有对此进行彻底的测试.

I am using Ubuntu on WSL2.

According to How to fix docker ‘Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?’ on Ubuntu, I can automatically start the docker daemon at boot using

sudo systemctl enable docker

instead of just starting it again at every boot with

sudo systemctl start docker

with both commands avoiding "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?".

When using any of the two, I get

Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker

and a test run shows, that docker is not yet running:

docker run hello-world 

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. See 'docker run --help'.

Some steps before, I also got a different message at this point:

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

which brought me to Fixing "System has not been booted with systemd as init system" Error:

Reason: Your Linux system is not using systemd How to know which init system you are using? You may use this command to know the process name associated with PID 1 (the first process that runs on your system):

ps -p 1 -o comm=

It should show systemd or sysv (or something like that) in the output.

ps -p 1 -o comm= gave me init.

According to this and this table

I can choose service docker start to run docker, which works. But I cannot find something like "systemd"'s sudo systemctl enable docker for "sysvinit". I would expect it to be like:

sudo service docker enable

But that "enable" is not available for "sysvinit" / "init".

While sudo service docker start works like sudo systemctl start docker, there is no such command that uses "enable". At the moment, I need to run sudo service docker start whenever I start WSL2.

The question:

What is the command that reaches sudo systemctl enable docker using sudo service docker ..., or if that does not exist, what is a workaround here to automatically start docker when opening Ubuntu on WSL2?

解决方案

Short answer to "what is a workaround here to automatically start docker when opening Ubuntu on WSL2?

  • Run the necessary commands in your user startup scripts (e.g. .profile). Do it with a check to see if the service is running first, like:

    sudo service docker status || sudo service docker start.

  • visudo or add rules to /etc/sudoers.d to allow your user to run the commands without a password:

    username ALL = (root) NOPASSWD: /usr/sbin/service docker *

More Details:

As you've discovered, WSL does not include any systemd support, nor really any direct support for starting a service on boot.

For starters, the WSL subsystem doesn't launch at Windows boot, but only when the user launches a login session anyway. So without any real "system start", the init.d or systemd startup doesn't make as much sense.

Further, users may have multiple WSL instances/distributions running, and if you are doing that (as I am), then you really don't want all services from all instances running on every boot.

For Docker, though, are you running Docker Desktop with WSL2 integration, or just installed directly into a WSl2 instance? For Docker Desktop, I ran across this in another question yesterday on how to start Docker Desktop daemon at Windows boot.

You can also have the WSL2 instance start via Windows Task Manager when the user logs in, and run the script via something like wsl sudo service docker start in the Task Manager.

Note that the same doesn't seem to work at Windows boot, however, (only login) because Windows seems to terminate any WSL instance that isn't tied to an active user after a few seconds (even if a service is running in the background). You can work around this with the PowerShell Invoke-WmiMethod, something like ...

powershell.exe Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList 'wsl', although I haven't tested this all that thoroughly.

这篇关于"sudo systemctl enable docker"不可用:在WSL2上启动时自动运行Docker(使用"sysvinit"/"init"命令或解决方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆