22

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.

0

3 Answers 3

34

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I didn’t expect this to work at first but somehow it solved the issue. Thanks for your help!
This was unexpected, saved me a lot of hassle, thanks!.
the solution to unset it works, but it is not set in .bashrc. Any other clues where it may be set?
try ~/.bash_aliases
I just needed to close and reopen my terminal after verifying my user was in docker group
15

For me below commands worked (execute them in order)

sudo addgroup --system docker
sudo adduser $USER docker
newgrp docker
# And something needs to be done so $USER always runs in group `docker` on the `Ubuntu` WSL
sudo chown root:docker /var/run/docker.sock
sudo chmod g+w /var/run/docker.sock

Reference : https://github.com/rancher-sandbox/rancher-desktop/issues/1156#issuecomment-1017042882

Comments

5

For those who want to know a bit more about why the client doesn't work initially without sudo :

The Docker daemon binds to a Unix socket, not a TCP port. By default it's the root user that owns the Unix socket, and other users can only access it using sudo. The Docker daemon always runs as the root user.

Try the following commands:

 sudo groupadd docker
 sudo usermod -aG docker $USER
 newgrp docker

Source and solution : https://docs.docker.com/engine/install/linux-postinstall/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.