78

I am running WSL 2 (Ubuntu) on a Windows 10 machine. I installed Docker on my Ubuntu using the official Docker guide (https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository). Running docker run hello-world produces the following error: docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

I have tried the following steps:

  1. Using sudo docker... instead of docker...
  2. Adding user to docker usergroup with sudo usermod -aG docker $(whoami)
  3. sudo nohup docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock & which I found in this thread: https://forums.docker.com/t/cannot-connect-to-the-docker-daemon-is-the-docker-daemon-running-on-this-host/8925/4

None of these steps helped.

docker version produces this output:

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

service docker status gives Docker is not running. service docker start/restart does nothing, Docker does not start. Any ideas what could be the issue?

Kris Stern
  • 1,192
  • 1
  • 15
  • 23
PalBo
  • 2,203
  • 3
  • 22
  • 43
  • Just hit same issue myself... must be something to do with how you've set groups up because I've also followed along with https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9 but elected not to setup groups and hit same issue. Solution was to just run the docker command as root (i.e. sudo the command and voila) – Daniel Machet Sep 18 '21 at 17:28
  • 8
    Running `$ sudo /etc/init.d/docker start` as byte mamba said below solved my problem. – Felipe Windmoller Dec 31 '21 at 18:55
  • 3
    This did the trick: `sudo update-alternatives --set iptables /usr/sbin/iptables-legacy sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy` – feitzi Jun 22 '22 at 09:25
  • This worked for me: **sudo service docker start** – deadParrot Nov 04 '22 at 18:08
  • You can try to start the docker daemon manually : just type "sudo dockerd &" and go in another terminal and type a docker command. – Allenile Feb 15 '23 at 17:01

2 Answers2

86

Using WSL2

You simply have to activate and use WSL2, I have to install Ubuntu 20.04 as the 18.04 wasn't connecting with Docker desktop. In the windows shell:

To check the WSL mode, run

wsl -l -v

To upgrade your existing Linux distro to v2, run:

wsl --set-version (distro name) 2

WSL Integration will be enabled on your default WSL distribution. To change your default WSL distro, run

wsl --set-default <distro name>

Then in docker you have to.

...use the WSL2 engine Activate WSL2

...access from your default WSL2 enter image description here

Based on this article. A Linux Dev Environment on Windows with WSL 2, Docker Desktop And the docker docs. Docker Desktop WSL 2 backend

Below is valid only for WSL1

It seems that docker cannot run inside WSL. What they propose is to connect the WSL to your docker desktop running in windows: Setting Up Docker for Windows and WSL

In the docker forums they also refer to that solution: Cannot connect to the docker daemon

starball
  • 20,030
  • 7
  • 43
  • 238
J.Serra
  • 1,208
  • 9
  • 11
  • 3
    Upgrading Ubuntu 18.04 -> 20.04 was all I needed – serg06 Nov 24 '20 at 15:30
  • I also had to enable the integration with the distro installed (in my case it was Ubuntu 20.04) – Vito Gentile Dec 02 '20 at 15:35
  • 25
    This doesn't answer the question. @PalBo asked how to use Docker when it's installed through the normal Ubuntu installation process, not when using Docker Desktop installed on the Windows operating system containing the WSL 2 distro. – Matt Welke Jul 27 '21 at 02:40
  • For Windows 10 Home it is not possible to install the Docker Desktop (error: "HyperV is not available on Home editions. Please use Docker Toolbox"). Of course, Toolbox is deprecated, no longer available on the site. Instead, followed this: https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps, yet step #5 may not work because the "Legacy" distro is in use. Instead, followed this post's instructions with the exact PowerShell commands: `wsl --set-version Ubuntu 2` and after finishing converting, `wsl --set-default Ubuntu 2`. That was it, WSL2 can run hello-world. – Kiddo Aug 17 '21 at 18:22
  • 8
    This command can solve my problem: `$ sudo /etc/init.d/docker start` – 取一个好的名字 Oct 11 '21 at 02:06
  • doesn't work for me at least on Win 11 – Aditya Jan 12 '22 at 14:58
  • Could you please replace the images with text? The images do not load in our corporate environment due to proxy policy. – rAhulD Feb 03 '22 at 09:05
  • 1
    Similarly to what the comment above mentioned, if you want to actually run docker in ubuntu, running `sudo dockerd` worked for me (it will run the docker daemon so you need to open up another terminal after that, or run it in the background ... Also, I wasn't able to login into a running docker image this way, but the images would run) – JSoet Apr 06 '22 at 20:15
  • Fixed my issue on win10 with wsl ubuntu 18.04 LTS – Matt - Block-Farms.io Jul 23 '22 at 06:16
  • 'docker start' did it. How silly of me not to realize that, lol. – MostHated Oct 11 '22 at 18:43
  • Thanks alot, this sovled my problem. I am using WSL1 for some reasons and WSL1 and I have installed Docker on Windows (using WSL2). Now I can use Docker (installed on WSL1) on my WSL1. – Syed Rafay Dec 06 '22 at 01:19
  • Thanks, I executed `wsl --set-version Ubuntu-22.04 2` and then it worked. Strange that this is the only place online I've found this info despite searching for eons. – Gruber Mar 07 '23 at 12:14
10

Go into PowerShell and type: docker context ls

Output:

NAME                DESCRIPTION                               DOCKER ENDPOINT                  KUBERNETES ENDPOINT   ORCHESTRATOR
default *           Current DOCKER_HOST based configuration   npipe:////./pipe/docker_engine                         swarm

Go back into your wsl/ubuntu setup and type:

export DOCKER_HOST=unix:///var/run/docker.sock

This should automatically show your full output when typing docker version.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Bioblaze Payne
  • 364
  • 3
  • 10