WSL - Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Hi guys.

I’m using WSL on Windows 11 and installed Docker following instructions from Docker documentation - Install Docker Engine on Ubuntu | Docker Documentation

renatospaka@LINA-QG:~/dev$ docker version
Client: Docker Engine - Community
 Version:           20.10.9
 API version:       1.41
 Go version:        go1.16.8
 Git commit:        c2ea9bc
 Built:             Mon Oct  4 16:08:29 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

However, it is not working as per the following error:

renatospaka@LINA-QG:~/dev$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

This is my WSL/Ubuntu setup:

renatospaka@LINA-QG:~/dev$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Regards,
Renato

1 Like

Hi, WSL does not have systemd. I have Windows 10, but I don’t think it is different in Windows 11.
Every time you start the WSL distro you have to start the docker service:

sudo service docker start

1 Like

Hi ,
correct, WSL does not have systemd, It is said on the Internet that it may also be because the docker service is not started, so I used another command:
sudo service docker start
and system shows: * Starting Docker: docker
it seem like run, I run this command:
sudo service docker status
but system show : * Docker is not running
I tried lots of time but it still not running…,can you help me, thank very much!
regards,
DB

1 Like

It was just yesterday when I helped a colleague with the same issue. In his case WSL got an IP address colliding with the Docker network, so he had to change either the network of WSL or Docker. Changing Docker network was easier, so I recommended to do that. Create or edit /etc/docker/daemon.json and add an address pool setting:

{
  "default-address-pools": [
    {
      "base": "192.168.0.0/16",
      "size": 24
    }
  ]
}

It could also be:

{
  "default-address-pools": [
    {
      "base": "10.10.0.0/16",
      "size": 24
    }
  ]
}

or anything that is available in your network.

It is just an example. Make sure you don’t set a pool colliding with your local network. If you already tried to change the network of WSL, reset all the changes and reboot Windows so it can create the WSL network again.

The other, harder approach is creating a network adapter on the host which covers the IP addresses of Docker, so WSL can’t use that and it will choose an available IP address from 192.168.0.0/16.

Update

I realized that the described configuration above was not the solution of the issue, but it happened during the configuration which was required to fix the network. At a point, Docker could not start. You can check the log file:

sudo cat /var/log/docker.log
1 Like

In my case the problem was that the installation of docker expects to listen events from another socket.

I went to /var/log/docker.log and the last line of the log was saying:

“API listen on /mnt/wsl/shared-docker/docker.sock”

So wat i did was: go to /var/run and run:

sudo rm docker.sock && sudo ln -s /mnt/wsl/shared-docker/docker.sock /var/run/docker.sock

Basically I created a symbolic link between the two sockets. This fixed the issue.

1 Like

Thank you! This resolved my problem to run docker with “-H” flag every time.

Detailed answer provided here as well

Cannot connect to docker daemon

  1. Check that WSL distro is enabled for Docker integration. In Docker Desktop, go to Settings > Resources > WSL INTEGRATION and make sure that the checkbox next to your WSL distro is checked.
10 Likes

Run sudo update-alternatives --config iptables
Enter 1 to select iptables-legacy
Now run sudo service docker start, and Docker will start as expected!

6 Likes

Thank you, @quangsangle, you are a genious - This was the easiest way to resolve the problem instead of manually modifying files.

Minor update about WSL not supporting systemd: it does since end of 2022.

It must be set inside the WSL2 distribution by editing the file /etc/wsl.conf (it requires root privileges to edit the file)

[boot]
systemd=true

Once the distribution is terminated and restarted, docker-ce will be started by systemd.

1 Like

Thank you very much @quangsangle, your method worked, fast and easy.


Moderator rnote: This message was translated by Rimelek using Google Translate.

This fixed my problem, which was very similar to the one described by dockback, thank you very much!

I just tried out your solution and here is my result.

So you didn’t look in the docs at all? Either prefix your command with sudo or apply what is written in the post installation steps and then use the docker cli without sudo.

In my case, the checkbox for “Enable integration with my default WSL distro” in Docker settings/resources somehow unchecked itself. Rechecking and restarting resolved my issue.

2 Likes

I don’t really have much idea what the command does but worked fine for me . Tysm :slight_smile:

I got the same issue. I tried the above but no success, Please help