Dutch77
Dutch77

Reputation: 1047

Why am I getting a "Cannot connect to the Docker daemon" error in WSL2?

Here is my setup:

I am currently forced to use Windows for development, so I became a Microsoft insider member and installed ubuntu with WSL2 mode. Docker desktop supports integration for WSL2, so I tried it...

For a week it worked flawlessly. Today after a PC restart, I can't get docker running again. Ubuntu can see the injected binaries from Docker desktop, but it can't connect to windows hosted docker daemon anymore.

When I call in the WSL terminal docker info it returns

$ docker info
Client:
 Debug Mode: false

Server:
ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
errors pretty printing info

or with docker-compose up

ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

What I've tried already:

I really, really need this to work. Thanks for any ideas. Weirdest thing is it worked yesterday and I didn't make any changes in system from then...

Upvotes: 81

Views: 128515

Answers (18)

Nagev
Nagev

Reputation: 13285

I had installed Docker for Windows, as recommended, to use it with WSL 2 and that does indeed start the docker daemon for you. But I don't need all the fancy features it offers so removed it and was pleased to see about 4GB freed and no extra icon in the system tray.

Now if I need to run docker commands I just begin with:

sudo dockerd &
sudo dockerd > /dev/null 2>&1 &  # or this to suppress output

This way I can have it running on the background on the same shell. Note that in this example I have setup sudo without password. If a password is required, I can do sudo dockerd and open another terminal tab.

Although this works as a quick temporary solution I've seen it cause network issues. In one machine I prefer using a light VM instead.

Upvotes: 13

Updating the Docker desktop in Windows worked for me! Not sure why no one mentioned

Upvotes: 0

Pierre Louis
Pierre Louis

Reputation: 410

Try launching the docker service inside wsl2:

sudo service docker start

Verify the server is running

docker info

Upvotes: 1

Miral
Miral

Reputation: 13095

The following sequence worked for me:

  1. Turn off all the WSL Integration checkboxes.
  2. Apply & Restart
  3. Turn back on either just the Default checkbox, or the specific distro slider, depending on whether it's the default or not.
  4. Apply & Restart
  5. Right-click the docker tray icon and Restart Docker.

It wasn't until step 5 that it actually started working.

I did have a container running at the time and "Apply & Restart" happened suspiciously fast, so I suspect it forgot to do the actual restarting part.

Upvotes: 0

lacostenycoder
lacostenycoder

Reputation: 11226

I found a 1.5 step fix. Go here, uncheck Enable, then Apply & restart button. Then check Enable, then Apply & restart button.

Docker now working fine in WSL2 Ubuntu.

enter image description here

Upvotes: 5

Patricio
Patricio

Reputation: 946

I know this may be outdated for the present question, but this should save us precious time, especially when Windows 20H1=2004 is going to Production this month (May 2020).

  Operating System Version: Windows 10 Education (Same as Enterprise and a superset of Pro).
  Version: 2004
  Build (Version OS): 19041.264
  Others: Windows Feature Experience Pack 120.2202.130.0.
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Shell: WSL Terminal

First, I have installed WSL v1 previously, then executed the procedure to upgrade to WSLv2, and this error shows up: "ERROR: Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?".

Second, to fix that error, I followed instructions stated here: Link, and it worked.

Third, after some tests I think the missing change in the upgrade, was removing the DOCKER_HOST variable from shell's start script.

SUMMARY: In my case, the procedure for a permanent fix should be the following STEPs:

1. Test if it's your case unsetting DOCKER_HOST variable (See image below).

2. If the error disappears with previous step, then time to fix changes by removing the setting of the DOCKER_HOST variable in the shell's start script (In my case was *$HOME/.bashrc*).
  Commented this out:
  #export DOCKER_HOST=tcp://localhost:2375
  #export DOCKER_BUILDKIT=1

  NOTE: Also include DOCKER_BUILDKIT.

3. Close and open the Terminal.

  Test in Step 1: enter image description here

Good luck!!

Upvotes: 74

Nhamza
Nhamza

Reputation: 31

I had the same problem, the solution for me was to set my Ubuntu as the default wsl distribution: wsl --set-default Ubuntu

Upvotes: 3

1.open windows docker desktop --> Setting -->General --> Disable Expose daemon on tcp://localhost:2375 without TLS

img1

2.and then Go to Settings --> Resources --> WSL integration --> uncheck Enable integration with my default WSL distro and turn off integration with distro

img2

3.click apply and restart

4.then go to ubuntu try docker ps
docker ps if it does not work, continue to run the following command unset DOCKER_HOST to disable DOCKER_HOST

img3

Today I just tried it successfully

good luck to you

Upvotes: 10

UltimaWeapon
UltimaWeapon

Reputation: 790

The accepted answer is mostly correct. However, I wanted to specify that when using WSL2 + Docker Desktop:

  • You must unset DOCKER_HOST which was previously needed in WSL1
    • Mine was defined in ~/.bashrc in both Windows and WSL.
    • Delete in both. Also delete in Windows env variables via Control Panel.
    • Check with env | grep -i docker to make sure it's gone.
  • You must also set the correct settings in Docker Desktop
    • Uncheck Export daemon on tcp://localhost:2375 without TLS
    • Check Use the WSL2 based engine
    • Resources -> WSL Integration -> Check Enable integration with my default WSL distro

Now, you can do a simple docker info to check if you're running the same server version in WSL and in Windows (Powershell).

Upvotes: 2

J Aravindhan
J Aravindhan

Reputation: 21

After hours, my docker worked using following method.

  1. Go to docker desktop --> Setting -->General --> Disable Expose daemon on tcp://localhost:2375 without TLS

  2. Go to Settings --> Resources --> WSL integration --> uncheck Enable integration with my default WSL distro and turn off integration with distro

  3. Restart Docker desktop

  4. Now in WSL,

    unset DOCKER_HOST
    
  5. Now try,

    docker ps
    

Upvotes: 2

Rahuldeb Chakrabarty
Rahuldeb Chakrabarty

Reputation: 41

There is another very basic catch:

  • Ensure virtualization is enabled in the BIOS.
  • Please enable the Virtual Machine Platform Windows feature from the selection of additional Windows Features.
  • Now my motherboard is being very old, the BIOS does not support Enabling Virtualization.
  • Hence no solution will work for me.
  • WSL Version 1 or 2 will come from Windows Update automatically.

Upvotes: 0

Yago Dórea
Yago Dórea

Reputation: 345

I've tried soooo many things, and the stuff that worked for me, and no one ever mentioned to try:

(from Windows Powershell)

wsl --set-default <my-distro>

then and there, I could connect to docker without changing the DOCKER_HOST var.

Upvotes: 15

amadesclaire
amadesclaire

Reputation: 555

On the Docker Desktop app I had to manually enable my distro integration under

Settings > Resources > WSL Integration

Upvotes: 43

petero124
petero124

Reputation: 1

For whatever it's worth (this is an old thread). Maybe someone else is still desperately trying to solve this puzzle.

I have just stumbled over the solution in my case. I am running the following

  • docker desktop version 3.3.3
  • wsl 2
  • Fedora 33 Over and over again I ran into this issue "Cannot connect to the Docker daemon at unix:///var/run/docker.sock". Reinstalled, restarted, blablabla.

My ultimate error were access rights on /var/run/docker.sock, and I am running wsl under my personal user srw-rw---- 1 root docker 0 May 7 10:29 /var/run/docker.sock

So if I run as root (sudo docker info) or I put myself into group "docker" (sudo usermod -aG docker $USER) I'm all well. Please look here https://docs.docker.com/engine/install/linux-postinstall/

Upvotes: 0

Abdelrahman Farag
Abdelrahman Farag

Reputation: 399

I have found my issue was due to mis-reading instructions., fixed on my windows version 1909 and WSL 2 with the following commands on CMD:

wsl.exe -l -v
wsl.exe --set-version ${distro-name} ${wsl version}

example:

C:\Users\xxxxx>wsl.exe -l -v
  NAME                   STATE           VERSION
* Ubuntu-18.04           Running         1
  docker-desktop-data    Running         2
  docker-desktop         Running         2

C:\Users\xxxxx>wsl.exe --set-version Ubuntu-18.04 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2

That's it

Upvotes: 4

guobiao_cn
guobiao_cn

Reputation: 65

ISSUE: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375

Powershell

wsl -l -v   # ALL DISPLAY "2"

Linux

unset DOCKER_HOST

/etc/init.d/docker restart

Restart Docker

windows docker restart
windows firewall off

Linux docker processes

docker ps

Upvotes: 2

oOMG
oOMG

Reputation: 1

you can consider upgrading your version to 19582.1000 , it's work for me.

See this issue.

enter image description here

Upvotes: -1

user2070077
user2070077

Reputation: 921

If it helps anyone else that is having this issue, for me it turned out that my subsystem was suddenly (and "on its own") ticked off in docker's RESOURCES > WSL INTEGRATION settings.

Upvotes: 82

Related Questions