Run dockerd as a background on WSL Ubuntu
I've installed Docker to WSL Ubuntu 20.04 distr. To make it run I need:
sudo dockerd
It runs but it blocks my terminal tab. To work with containers I need to open the second tab.
I've tried to use sudo dockerd &
- it seems to run dockerd in the background, but when I change a directory, it stops.
How can I run dockerd in the background and change directories without stopping?
The only way I have seen people do this is by running Docker in a VM (which doesn't seem to be an option you're considering) or running a script that is executed when WSL starts up.
Here's how you can have Docker run when WSL is started:
- Edit your
sudoers
file to allow your user account to start Docker without being prompted for a password:
Add this line:sudo visudo
Note: Be sure to changermarusyk ALL=(ALL) NOPASSWD: /usr/bin/dockerd
rmarusyk
to the user account you would like Docker to run under if it is not the same name as in your screenshot. - Edit your
~/.bashrc
file to check if Docker is running and, if it's not, start it:
Note: This can be added almost anywhere, but it's generally "cleaner" to have it at the end of the file so that anything that might be required ahead of time is configured/loaded/etc.# Start Docker (if not already running) RUNNING=`ps aux | grep dockerd | grep -v grep` if [ -z "$RUNNING" ]; then sudo dockerd > /dev/null 2>&1 & disown fi
- (Optional) If you would like to run Docker commands without needing to prefix them with
sudo
, you can add your user account to thedocker
group:sudo usermod -a -G docker $USER
- Test this works by closing the terminal, starting it up again, and checking on Docker's status:
Note: Feel free to use any Docker command.docker ps
This should do what you need 👍🏻
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
sincewsl.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
orwsl --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"