Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker client under WSL2 doesn't work without sudo

On WSL2 (Ubuntu 20.04), I'm trying to connect to the Docker daemon that's running on Windows.

$ docker ps
Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?
(exit code 1)

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
(exit code 0)

Why does it work with sudo, but not without sudo? How can I make it work without sudo?

I have done

$ sudo usermod -aG docker $USER 

which ran successfully, but didn't help with the issue.

I have also restarted everything many times, which didn't help.

like image 880
Prof. Legolasov Avatar asked Nov 06 '20 07:11

Prof. Legolasov


People also ask

Can Docker run without sudo?

The Docker daemon always runs as the root user. If you don't want to preface the docker command with sudo , create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

Why do I need sudo for Docker?

Running Docker commands with sudo ensures that the Docker command is executed with the security rights of root (by using sudo) or by a user who is a member of the docker group. In this quick tutorial, we'll learn how to run Docker commands without sudo.

Can I run Docker inside WSL2?

Advantages. It consumes the minimum memory needed to run Docker Daemon (Docker server). It's faster than Docker Desktop because it's running directly inside the WSL 2 instance instead of on a separate Linux instance.


1 Answers

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.

like image 161
reflectorfaculty Avatar answered Sep 26 '22 22:09

reflectorfaculty