2

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.

StoneThrow
  • 1,083
  • 3
  • 13
  • 22

1 Answers1

2

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.

NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
  • "the Docker Engine package under Ubuntu attempts to start the daemon via Systemd, which...isn't running by default on WSL": Systemd and SystemV init are the two (competing?) programs whose job is to "dispatch processes" given some form of init script (e.g. `init.d` scripts and `.service` files) -- true? So do you mean that Ubuntu WSL has chosen SystemV as its "process dispatcher"? And the docker engine install doesn't "work with" the SystemV mechanism to start `containerd`? – StoneThrow Jul 28 '22 at 22:45
  • 1
    I wouldn't say that "Ubuntu WSL has chosen SysV", but that *some* Ubuntu packages (even on "real" Ubuntu) still provide SysVInit scripts (which run easily even without a SysV "boot"). That's good for WSL, since it doesn't (easily) have Systemd support. For instance, in Ubuntu, for `cron`, there's both a Systemd unit file *and* a SysV script. The same for Docker. However, some packages *don't* continue to provide the legacy SysV scripts. On RPM-based distros, I believe you'll find even fewer SysV scripts. The WSL openSUSE Tumbleweed distro, for instance, has no `/etc/init.d` at all. – NotTheDr01ds Jul 28 '22 at 22:54
  • 1
    Also recommend reading my [Systemd doesn't work on Ubuntu/WSL deep-dive](https://askubuntu.com/a/1379567/1165986) answer on Ask Ubuntu. – NotTheDr01ds Jul 28 '22 at 22:55
  • Forgive me if I'm simply rephrasing what you've already stated...I'm just trying to confirm my correct understanding of your answer...so in this case, the Docker Engine "install package" (what gets run/installed by `apt-get`) is one of those Ubuntu packages that's chosen to only update SysD init scripts, and not SysV init scripts? I.e. I should see identical behavior in any Ubuntu environment (WSL or "real") that boots with SysV (not SysD) if I had run the same steps to install Docker Engine? – StoneThrow Jul 28 '22 at 23:07
  • 1
    @StoneThrow No worries, but we may want to take this over to chat at some point. I'd recommend maybe the [Ask Ubuntu General Room](https://chat.stackexchange.com/rooms/201/ask-ubuntu-general-room). But let me say it slightly differently for clarity -- The Docker package itself comes with *both* Systemd unit files and a legacy SysV init script. However, the package install script *assumes* that it is running on a Systemd system and uses `systemctl enable docker` (to make it run at boot) and `systemctl start docker` (to make it run in the current boot). Neither of those commands work on WSL. – NotTheDr01ds Jul 28 '22 at 23:14
  • If you head to chat, remember to @tag me so I'll get a notification. Thanks! – NotTheDr01ds Jul 28 '22 at 23:14
  • The command `service docker start` returns `docker: unrecognized service` at least on Ubuntu 22.04. Starting it with systemctl doesn't work either, and unfortunately this answer does not cover how to enable systemd on WSL. I ended up starting the daemon manually, just type `sudo dockerd` on the command line. Works fine as a workaround. As always, make sure your user is in the `docker` group to access the socket. – fgiraldeau May 18 '23 at 20:00
  • @fgiraldeau Are you sure you used [the process](https://docs.docker.com/engine/install/ubuntu/) mentioned in the question? If installed from the official Docker repos, the latest version still has the init.d service for `sudo service docker start`. I just re-tested on a fresh 22.04 WSL2 distribution. If, however, you install from the Ubuntu repositories, then yes, `service docker start` will return `docker: unrecognized service`. That said, I'll also add a link on how to enable Systemd on WSL2. I tested that as well on 20.04 and 22.04 and it is working fine with the official Docker repos. – NotTheDr01ds May 19 '23 at 11:25
  • 1
    @NotTheDr01ds you are right, thanks for the follow-up. – fgiraldeau May 24 '23 at 23:57