Skip to content

Instantly share code, notes, and snippets.

@bartdorsey
Last active January 18, 2024 19:08
Show Gist options
  • Save bartdorsey/d6ad6782a36bdcf10fb0637769bec520 to your computer and use it in GitHub Desktop.
Save bartdorsey/d6ad6782a36bdcf10fb0637769bec520 to your computer and use it in GitHub Desktop.
Running Docker Inside WSL with systemd.

Running Docker inside WSL with systemd.

Why would you want to do this?

  • Because when you run docker desktop for Windows, it will be creating two WSL instances behind the scenes anyway. This will take up more resources than this method.
  • Docker desktop attempts to inject the docker commands into your existing WSL instance. It does this through a series of hacks that frankly are just complex and prone to breakage.
  • Bind mounts are from Linux to Linux. When you run Docker Desktop on Windows, you are bind mounting from the Windows filesystem to a Linux system. This can make things like hot reloading web frameworks not work very well. (this is because it can't recieve filesystem events when you save the files)

Why is this simpler?

Because it's exactly like running docker on a native linux machine.

Are there any downsides

You won't have access to the Docker Desktop UI. But you can use something like portainer if you absolutely need a UI. I personally just use the CLI. You should too.

Instructions

First, make sure you have WSL installed and the distro you want to run setup and configured.

Step 1: Enable systemd.

We first must turn on systemd. This is supported by editing the /etc/wsl.conf file and adding the following lines:

[boot]
systemd=true

One you edit this and save it, you will need to restart your wsl instance. A common way to do this is to run

wsl --shutdown

from a powershell.

When you open WSL back up, if you do a ps aux command you should see all the system processes like you would on a normal linux server.

Installing Docker

Next you want to install "Docker Engine" for Linux. Follow the instructions here for whatever Linux distro you are running:

https://docs.docker.com/engine/install/

There is no step 3

That's it, this works now exactly like docker on a regular Linux server.

Some tips

  1. Make sure you follow the instructions to allow your normal user to execute docker commands, otherwise you'll always have to use docker as root.
  2. The docker install script (if you use it), will complain about running in WSL and really wants you to use docker desktop. Ignore it, we know better than Docker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment