How to automatically start the Docker daemon on WSL2

I like running Docker containers in WSL2. However, one piece of nuisance for me has been that the Docker daemon doesn’t automatically start, and there’s no ‘easy’ way to start it automatically. Since WSL2 doesn’t run systemd, you cannot use systemd to automatically start Docker as you typically would in a Linux system.

In this post I’ll explore one way using which this can be done in WSL2 (and I assume this works for WSL1 as well, but I haven’t tested that).

Details of the system I’m running this on:

Automatically start Docker daemon on WSL2

First, you’ll need to install Docker.

sudo apt update
sudo apt install -y

With Docker installed, we’ll now need a way to run the Docker daemon automatically at boot time. One way this can be done is to run the command to execute to Docker daemon at boot time via your profile file. To do this without your command line prompting for passwords, run the following command:

sudo visudo

This will open your /etc/sudoers file, which controls how sudo command are executed. We’ll want to allow our user to start dockerd without being prompted for a password. Add the following line to the bottom:

nilfranadmin ALL=(ALL) NOPASSWD: /usr/bin/dockerd

(replace nilfranadmin with your username, unless you want to give me access to your system 🤣).

Now, we will update our profile file to automatically run the docker daemon if it’s not running yet. This can be done by adding the following lines to your profile. Please note, I’m using the zsh shell. If you’re using bash (like most people), you’ll need to change .zshrc by .bashrc.

echo '# Start Docker daemon automatically when logging in if not running.' >> ~/.zshrc
echo 'RUNNING=`ps aux | grep dockerd | grep -v grep`' >> ~/.zshrc
echo 'if [ -z "$RUNNING" ]; then' >> ~/.zshrc
echo '    sudo dockerd > /dev/null 2>&1 &' >> ~/.zshrc
echo '    disown' >> ~/.zshrc
echo 'fi' >> ~/.zshrc

Finally, add your username to the docker group, so you don’t need to use sudo to run Docker commands.

sudo usermod -a -G docker $USER

Logout and log back in (or open a new terminal) for all changes to take effect. With the changes in effect, you should be able to run:

docker run hello-world

And see the hello-world from Docker.


This was a short post, explaining how to setup your WSL system to automatically start Docker when you open your terminal. It’s a bit of a hacky solution, but it’ll make starting the Docker daemon a bit easier.

Leave a Reply