Can only run docker as root in WSL2

I have just upgraded to WSL2 on Windows 10 (latest build). I have installed the latest version of Docker for Windows (19.03.13), and uninstalled and re-installed the docker engine on my Ubuntu 18 image to match the Windows build. However, now I find I can only run docker container commands as root - prefacing everything with sudo. Otherwise I get an error message saying “Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?.”

I saw an article mentioning that the docker daemon runs as root and to run as user you have to add yourself to the docker group. However, this group already exists and adding myself to it hasn’t fixed it. I saw some posts saying you had to delete stale symlinks, but this didn’t help either.

I did encounter an article saying docker should in fact only ever be run as root, but I think this really refers to production environments. I’d be grateful if anyone could suggest a way of running docker as a regular user.

1 Like

Weird solution for this one - but go ahead and try:

unset DOCKER_HOST

And if that works, you can make the fix permanent by going back and commenting out the “export DOCKER_HOST=tcp://localhost:2375” in your .bashrc file. I think it has something to do with how docker is configured in WSL 2 vs. WSL 1, but Docker never updated their documentation to reflect this.

4 Likes

Thanks but in the end I concluded that upgrading to WSL2 had messed up my installation. I just downloaded a fresh one and started again. Also I didn’t understand that with WSL2 you don’t need a local installation of the docker tools.

after upgrading to WSL2 this worked to solve my user not needing to run it as sudo,
In WSL1 my user is added to the docker group, so i was able to run ‘docker ps’ without sudo.
After upgrading to WSL2 i could only run docker commands with sudo, even as root user i still have to run ‘sudo docker ps’

so true… un setting DOCKER_HOST does the trick…

To anyone experiencing this, check your dotfiles, including .zshrc, .bashrc, etc. to make sure that DOCKER_HOST isn’t hard-coded, or else it will conflict with WSL and cause this:

docker ps
Cannot connect to the Docker daemon at unix:///run/user/1000/docker.sock. Is the docker daemon running?

To check for DOCKER_HOST: grep -R DOCKER_HOST $HOME