The standard way to run the Docker Engine daemon (without Docker Desktop) under WSL Ubuntu is simply:
sudo service docker start
This handles the daemonization of it along with many other tasks. You can see the full script by examining /etc/init.d/docker
. Run this instead of trying to manually replicate the startup process.
If you want it to automatically start, there are several options:
You've already seen the other answer to modify your startup scripts. Personally, I'm not a huge fan of this method. First, I like to keep my startup scripts as lean as possible for best performance. Second, modifications like this tend to "accumulate" over time, making troubleshooting your shell more difficult in the future. That said, it's not a horrible option for WSL, at least on Windows 10.
However, if you really do want to go this route, there's a much easier way than the other answer. Simply add the following line to your ~/.bash_profile
(since it sounds like you are using Bash):
wsl.exe -u root -e sh -c "service docker status > /dev/null || service docker start"
That's all. You do not need to make modifications to your sudoers
since wsl.exe -u root
executes the session as the root user without needing a password.
But definitely use your ~/.bash_profile
for this, not ~/.bashrc
. The former is executed only for login shells, but the later is executed for all interactive shells, adding additional overhead.
Windows 11 makes this even easier (if you can upgrade) with a special configuration for services you want to run when the WSL instance starts. Just sudo vi /etc/wsl.conf
with the following contents:
[boot]
command="service docker start"
This will only execute once, when Ubuntu is started. If the instance is shutdown with wsl --terminate Ubuntu
or wsl --shutdown
, it will run again the next time you start Ubuntu.
These commands also run as root, with no password necessary.
If you'd like to start Docker Engine whenever you login to Windows, create a "Scheduled Task" in Windows which runs on Logon and points to wsl.exe
with the arguments being -u root -e sh -c "service docker status || service docker start"