Docker On Windows 10 - when you don't want to pay for it

So, it's just Docker Desktop, not the engine itself, you have to pay for. Grace period ends at the end of January 2022, but without the desktop application, it's not worth figuring out how to use with on plain old Windows-- enter WSL2 and Distrod.

WSL2 - Get Started

If you don't have WSL2 running on your Windows 10 (or 11), there are numerous articles, gists, and more on the Internet to get WSL2 installed.

To make use of this article, you'll need some WSL2 installed to even start, some Linux proficiency, and some knowledge of Systemd.

The Need For Systemd

Docker on Linux is basically runs as a systemd daemon. To get Docker working on WSL2, we need to get systemd working. WSL2 distros installed from the Microsoft Store currently do not support systemd.

Distrod is basically a hack to get Systemd running on WSL2. For vanilla WSL2 installs, the init system is a custom /init executable at the root– there are no hooks for systemd running on WSL2.

Distrod basically runs an entire WSL2 instance in a simple container with the init system patched to run systemd.

Installing Distro Using Distrod

  1. Download the latest zipfile, at the time of this writing, distrod_wsl_launcher-x86_64.zip and extract the launcher. Double clicking the launcher will launch the wizard installing any of its supported distros, giving it the default a WSL name Distrod. To override the default name run distrod_wsl_launcher.exe, from a PowerShell console using your own name in place of my_desired_name as shown below:
./distrod_wsl_launcher.exe -d my_desired_name
  1. You may want the wizard to show the list of available distributions. In case you want to change the name you already entered using the -d command line switch, and wish to use a new name based on the distro you decided want, just ^C to exit the Wizard and rerun using the desired name.
  2. Select Ubuntu Focal if you want ot use the same distro used for this tutorial, the procedure for setting up Docker will vary across distros.
  3. When you run the newly isntalled distro, you may be logged in as root. In that case create or edit the file /ect/wsl.conf and add the following setting myuser to the user you created installing the new distro:
[user]
default=myuser

Stop and restart the WSL distro after making this change.

Setup

Here we add the Docker registry to the Ubuntu repos, importing their key. After installing the docker-ce and docker-ce-cli packages, the current user is added to the docker group to run containers as a non-root user.

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"
$ sudo apt install -y docker-ce docker-ce-cli
$ sudo usermod -aG docker $USER

Shared Mounts

You may get errors trying to share volumnes between containers using docker-compose. If the findmnt command gives you get the following:

$ findmnt / -o PROPAGATION,TARGET
PROPAGATION TARGET
private     /

then running mount --make-shared as root may fix the problem until the next reboot. For a more permanent fix, create the systemd unit file like the following below:

/etc/systemd/system/make-shared.service

[Unit]
Description=I like my hierarchies shared
DefaultDependencies=no
Conflicts=shutdown.target
Before=local-fs-pre.target shutdown.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/mount --make-shared /

Run the following command ot enable the unit to run at boot.

$ sudo mkdir /etc/systemd/system/local-fs.target.wants
$ sudo ln -s ../make-shared.service /etc/systemd/system/make-shared.service
$ sudo systemctl daemon-reload

Wrap Up

Since Docker runs withing the WSL2 instance, you no longer need Docker Desktop. If Docker Desktop is installed, you may wish to keep it from starting up on Windows system startup.

References

  1. How To Install Docker on Ubuntu 20.04
  2. changing the "mount --make-shared /" default
  3. linux - Implications of mount --make-private / - Server Fault