How to Install Docker in Ubuntu WSL

dockerlinuxUbuntuwindows-subsystem-for-linux

I've recently installed Ubuntu 22.04 WSL on my Windows 10 PC.
My naive understanding is that this Ubuntu "app" is equivalent to a Linux development environment.
When I explore within the running Ubuntu app, it generally feels like the normal Linux I'm accustomed to: the shell appears to be bash, and I have access to executables like git, curl, man, etc.

Trusting this claim that this Ubuntu WSL is equivalent to a Linux development environment, I now want to run Docker in this Ubuntu instance.
My naive understanding is that "since Linux natively supports containers, I should install Docker Engine" on Linux.
So I followed the steps described here: https://docs.docker.com/engine/install/ubuntu/
But when I try to verify that Docker Engine is installed correctly by running the hello-world image, I get the following error:

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

There is no /var/run/docker.sock and I don't think the Docker daemon is running:

$ ls -l /var/run/docker.sock
ls: cannot access '/var/run/docker.sock': No such file or directory
$ ps aux | grep containerd
user    10212  0.0  0.0   8164   732 pts/2    S+   21:44   0:00 grep --color=auto containerd

Can someone please help me understand what has gone wrong, and if there is corrective action I can take to successfully install Docker in this Ubuntu WSL app on my Windows PC?

Perhaps are the instructions at https://docs.docker.com/engine/install/ubuntu/ only meant for "Ubuntu natively installed on a host PC" and not for Ubuntu WSL running on a Windows PC?

If so, then is there some other way I should be "installing Docker" so that Docker commands like sudo docker run hello-world can be run from within my Ubuntu WSL?

I'm quite confused by all the layers of software involved here.

Best Answer

My naive understanding is that this Ubuntu "app" is equivalent to a Linux development environment.

It's actually more like running Ubuntu in a container, because it really is. When running Ubuntu in a container (e.g. a Docker Ubuntu container), there are differences in how you set up services when compared to a physical or virtual machine. For instance, there's no Systemd running by default in a Docker Ubuntu container. There's no "login", and thus no PAM.

I've attempted to write up as many differences and limitations as I can think of in this Ask Ubuntu answer.

"since Linux natively supports containers, I should install Docker Engine" on Linux.

...

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

First, for most users, the recommended way of installing Docker in Windows/WSL2 is via Docker Desktop for Windows.

I cover some reasons why this is preferable in this Stack Overflow answer.

While it is possible to install and run Docker Engine manually in a WSL2 instance, there's one critical difference there that I explain in this answer. In short, the Docker Engine package under Ubuntu attempts to start the daemon via Systemd, which (as mentioned above) isn't running by default on WSL. On Ubuntu under WSL2, you need to first start the daemon with:

sudo service docker start

Updated:

You can now also utilize Systemd on WSL2 if desired to use the "documented" method. While I still prefer running without Systemd whenever possible on WSL2, there are definitely times when it can make life easier.

To enable, see the information in my answer here. Most recent WSL installations will have support built-in, but if you need to upgrade Windows or WSL, the instructions for that are in the post as well.

If you want to go this route, I do recommend that you enable Systemd before going through the Docker installation process linked in the question. With Systemd already running at the time of Docker install, it will automatically run and enable the service.

If you enable Systemd after installing Docker, you can run either:

sudo systemctl enable docker

... to have Docker automatically start when Ubuntu/WSL2 starts.

And/or:

sudo systemctl start docker

to start it in place of the service command.