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

17

75

Here is my setup:

  • Windows 10 PRO - build 19041.153 - insider program - slow ring
  • Ubuntu 18.04LTS subsystem in WSL2 mode
  • Docker for desktop 2.2.0.4 - enabled WSL2 integration with my Ubuntu subsystem

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:

  • expose daemon without TLS with envs like DOCKER_HOST=localhost:2375, DOCKER_HOST=tcp://localhost:2375, DOCKER_HOST=127.0.0.1:2375, DOCKER_HOST=tcp://127.0.0.1:2375 => same result
  • uninstall Docker desktop and install previous version
  • turn off windows firewall

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...

Yordan answered 16/3, 2020 at 14:55 Comment(8)
Installed another Ubuntu (16.04) and it works. Well thats cool, but how to fix my main subsys. :)Yordan
Did you try unset DOCKER_HOST? Sounds weird, but that's what did it for me.Surfboat
@Surfboat that did it for me!Eustazio
@Surfboat thanks did it for me too!Fireplug
unset DOCKER_HOST and restart docker desktop worked for me.Cupboard
maybe it helps someone: Check that you distro runs in correct version wsl -l -v. I accidentally changed the wsl version from 2 to 1 a long time before and did not remember it anymore.... if its the same for you wsl --set-version (distro name) 2 and afterwards restart docker-desktop will fix fix it.Melchizedek
unset DOCKER_HOST also worked for me !Deflation
Enabling WSL Integration and setting the distro install as default should be all you need. https://mcmap.net/q/54971/-why-am-i-getting-a-quot-cannot-connect-to-the-docker-daemon-quot-error-in-wsl2Cy
M
74

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!!

Migdaliamigeon answered 6/5, 2020 at 16:51 Comment(5)
For me it worked after unsetting the old variable (removed from .bashrc). Thanks!Acidforming
My question is why the heck wasn't this clearly documented by Docker in the first place!Staciastacie
Thanks @Migdaliamigeon your solution worked for me. My mistake was that I installed docker-compose inside WSL when Docker Desktop is already managing all that!Somato
Following the instructions in the linked article worked for me. I had no DOCKER_HOST environment variable so I needed to follow the full instructions and install Docker Desktop Edge and it works great.Marxism
For me the key was uninstalling docker-ce and docker-ce-cli which was still present as i was using WSL v1 before. Best hint from the linked ressource: "Some of the available tutorials or advices can be confusing because this time you shouldn't install or configure anything related to Docker under your WSL Linux distribution."Isomerous
F
71

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.

Falk answered 23/4, 2020 at 0:51 Comment(9)
This happened to me when upgrading from the Stable to Edge release.Tremulous
Same for me. It happened when I added ubuntu 20 next to ubuntu 18Harewood
same here.. * ticked it on again * run wsl --shutdown * started WSL2 distro again * still docker ps responds with Cannot connect to the Docker daemon at... Was a restart required for you guys?Predella
@Predella i restarted and it still didn't work for me, did you have any luck?Crackdown
Happened to me when I moved my VM's storage file through wsl commands, i.e. unregistering it and importing it again removes it from docker integration.Meristic
Sneaky untick after docker update. Wsl could connect again after manual tick :/Adenitis
This is exactly what happened to me. I ticked the Ubuntu integration back, and after waiting ~30s it worked again. Thanks a lot dude!Midmost
Just happened to me as well in Docker Desktop 4.27.2Yser
Yep just happened to me in 4.27.2Melioration
H
34

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

Settings > Resources > WSL Integration
Hammel answered 14/7, 2021 at 14:13 Comment(1)
This solved it for me more than once! If you've done this before, check it again. I found the checkbox for "my default WSL distro" was checked, but the switch for "Ubuntu" under "additional distros" went off.Hacking
R
14

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.

Ropedancer answered 28/2, 2022 at 15:26 Comment(2)
This should be much higher in the results. This and enabling WSL Integration should be all you need.Cy
This answer is easy and working.Eurystheus
P
13

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 &

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 so I would not recommend it, and prefer using a light VM instead.

Palmer answered 29/9, 2021 at 15:21 Comment(0)
D
10

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

Diatomaceous answered 14/6, 2022 at 12:59 Comment(1)
Worked for me with Windows 11 and Arch on WSL2.Ecklund
B
4

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

Bulldoze answered 23/11, 2020 at 13:50 Comment(1)
This is the only thing that worked for me, but only after I updated my Windows with "Check for Updates", on 1909 Build 18363.1256 - works like a charmExtraneous
L
3

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

Lalapalooza answered 15/3, 2023 at 14:31 Comment(1)
Thank you!! this was the only thing that helped and I tried a billion things before thisCenturion
E
2

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
Eveleen answered 11/10, 2020 at 18:44 Comment(2)
I get >'windows' is not recognized as an internal or external command, operable program or batch file.Alarmist
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?Periodate
H
2

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

Huberman answered 10/11, 2022 at 15:18 Comment(1)
you are genuis, thanxTokoloshe
L
1

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
    
Lali answered 24/5, 2022 at 20:5 Comment(0)
E
1

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).

Epifocal answered 25/5, 2022 at 20:28 Comment(2)
As well as this answer: wsl -l (in PowerShell) tells me wsl default is being reset to docker-desktop-data (Default) I repeatedly have to flick the switch in Docker Desktop under Settings -> Resources -> WSL Integration to "Enable integration with additional distros:". or wsl --set-default Ubuntu in PowerShellHacking
I say "repeatedly" because I think these settings are lost when I restart Docker Desktop, or Windows.Hacking
M
0

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/

Morgen answered 7/5, 2021 at 8:39 Comment(0)
S
0

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.
Silassilastic answered 16/3, 2022 at 19:4 Comment(0)
D
0

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.

Divebomb answered 16/3, 2023 at 5:44 Comment(0)
P
0

Try launching the docker service inside wsl2:

sudo service docker start

Verify the server is running

docker info
Phooey answered 8/6, 2023 at 17:4 Comment(0)
N
-1

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

See this issue.

enter image description here

Nutritionist answered 16/3, 2020 at 16:4 Comment(3)
Moved to build 19582.1001. Same issue :(Yordan
you should upgrade to Docker Desktop WSL 2 backend docs.docker.com/docker-for-windows/wsl-tech-previewNutritionist
Did that. No luck. But it doesn't matter anymore. I deleted whole Ubuntu and started on clean slate and it's working. (for now). Thank you very much for reponses :)Yordan

© 2022 - 2024 — McMap. All rights reserved.