Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"sudo systemctl enable docker" not available: Automatically run Docker at boot on WSL2 (using a "sysvinit" / "init" command or a workaround)

I am using Ubuntu on WSL2 (not on Docker Desktop).

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

enter image description here

Systemd command
    Sysvinit command

systemctl start service_name
    service service_name start

systemctl stop service_name
    service service_name stop

systemctl restart service_name
    service service_name restart

systemctl status service_name
    service service_name status

systemctl enable service_name
    chkconfig service_name on

systemctl disable service_name
    chkconfig service_name off

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?

like image 604
questionto42standswithUkraine Avatar asked Dec 08 '22 09:12

questionto42standswithUkraine


1 Answers


This answer requires the latest version of Windows and WSL at the time of this posting, and it now works under both Windows 10 and 11. Run wsl --version and confirm that you are on WSL 1.0.0 (not to be confused with WSL1) or later.

If you are on an older release of Windows or WSL, then wsl --version will likely just show the Help text. See this answer for information on how to upgrade.

If you cannot upgrade at this time, then please see my original answer for a workaround for Windows 10.


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

  • Option 1: Enable Systemd support in WSL2

    The latest release of WSL2 includes support for Systemd. You can read how to enable it in this Community Wiki answer or my original Ask Ubuntu answer.

    However, my personal recommendation is to consider whether you really need Systemd. It will add additional overhead and potentially other complications, and it isn't strictly necessary for Ubuntu to run (well) on WSL, as we've been doing for quite a few years without it. Option 2 may be a better (and faster) option for many services.

    If you do have Systemd enabled, then the commands in the original question should work for you:

    sudo systemctl enable docker
    sudo systemctl start docker
    

    Docker Engine should automatically start for you the next time you restart your WSL2 distribution. However, please see the bottom of this answer for an important note on keeping the services running.


  • Option 2: Add the necessary commands to the [boot] section in /etc/wsl.conf:

    [boot]
    command= service docker start
    

    To run multiple commands, separate them with a semicolon as in:

    [boot]
    command= service docker start; service cron start
    

Important Note: If you run a service (e.g. cron or docker) using either of these methods, please note that the WSL distribution will still auto-terminate when the last process that was started interactively completes. You can see more discussion (and a workaround using keychain) for this in my answer to the Ask Ubuntu question Is it possible to run a WSL app in the background?.

like image 68
NotTheDr01ds Avatar answered Dec 12 '22 04:12

NotTheDr01ds