Skip to main content

WSL2 ;)

How to automatically start the Docker daemon on WSL2

Identify user

$ whoami

We’ll want to allow our user to start dockerd without being prompted for a password

$ sudo visudo
/etc/sudoers
usuario ALL=(ALL) NOPASSWD: /usr/bin/dockerd

Now, we will update our profile file to automatically run the docker daemon if it’s not running yet.

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

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

Stop WSL2 (powershell)

> wsl --shutdown

Execute Docker in WSL2 (ubuntu)

$ docker run hello-world