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

You have followed instructions to install Docker on Ubuntu, which is a real Linux distribution. This does not suite WSL, because Docker has to be installed in Windows.

The Microsoft description is found in the article Get started with Docker remote containers on WSL 2.

The article lists the prerequisites:

  • Windows 10 at least version 2004
  • Enabled WSL, install a Linux distribution, and update to WSL 2
  • The Linux kernel update package.

Once this is done:

  • Install Docker Desktop in Windows
  • Enable WSL integration with the Docker engine that is installed in Windows.

The details are found in the article, the above is only a short summary of the steps.

The Docker documentation for installation on Windows is found in Docker Desktop WSL 2 backend and might be useful to complete the Microsoft article.


Mostly a copy/paste from a similar issue I answered recently on Stack Overflow:

You are installing Docker Engine directly on Ubuntu WSL, which is fine, but I want to be sure you understand that you have another option. The "recommended" method (as @harrymc pointed out) is to install Docker Desktop for Windows.

Docker Desktop does provide a few additional features over the base Docker Engine:

  • It can be shared amongst multiple WSL2 instances
  • It can run from PowerShell and CMD
  • It provides a GUI dashboard of containers and volumes
  • It handles automatic upgrades (although some might not consider that necessarily an advantage)
  • It's a convenience method that handles all of the other stuff below automatically for you.

Both versions run the same engine underneath, but Desktop just provides those additional convenience features as well. If you don't need or want these, and you really do want to just install Docker Engine, then the solution to your current issue should be to just run:

sudo service docker start

Then try the sample again. It's an extra step that's required in WSL that isn't covered in the Docker doc.

(Bonus tip) I recommend running hello-world via:

sudo docker run --rm hello-world

There's no reason to leave that particular container hanging around after it exits, IMHO. I wish Docker would update those docs.

Also, assuming everything works at that point, I recommend getting rid of that image via:

sudo docker rmi hello-world

Explanation:

On "regular" Ubuntu, part of the installation process performed by the docker-ce package is to start the Docker daemon. However, that step fails due to differences in the way WSL works (lack of runlevels, systemd, startup script support, etc).

So when you get to that (currently) Step 3 to verify that the daemon is running via docker run ..., it's not.

You're also going to run into a similar issue when you get to the section "Post-installation steps for Linux: Configure Docker to start on boot"*

This won't work on WSL, since there's no concept of a "boot" (and, again, no Systemd). So you'll need an alternate method to make sure the engine is running in WSL. That can, of course, simply be a manual sudo service docker start when you need it. Or you can, if you'd like, add something like the following to your ~/.bashrc:

wsl.exe -u root -e sh -c "service docker status || service docker start"

There are other methods as well, but that's just one example.