Running Containers on Windows Subsystem for Linux (WSL 2)

How to install and automatically start Docker Engine on WSL distributions such as Ubuntu.

Jonathan Merlevede
datamindedbe
3 min readMar 3, 2022

--

The changed licensing model of Docker Desktop has contributed to the quick rise in popularity of excellent alternatives for Windows and Mac, such as Podman and Rancher Desktop. Despite this, Docker often remains your best option for running containers. This story details how to use the free Docker Engine from WSL.

Docker — still one of the best options for developing and running containers.

Background

A typical Docker installation on Windows consists of Docker Desktop, installed on top of Windows Subsystem for Linux (WSL). This allows running containers both from Windows and your WSL distros. Unfortunately, Docker Desktop is no longer free for commercial use in larger companies.

You may not need the ability to run Docker from Windows. If this is the case, you can install Docker Engine, also known as Docker Community Edition (CE), inside of WSL instead. Docker Engine is free, but available only for Linux.

This story assumes the use the Ubuntu WSL distribution, but the same instructions should work on other distributions too.

Docker installation

Installing Docker inside of WSL is not hard; simply run the following command inside of your WSL distribution:

curl https://get.docker.com/ | sh

The installer will ask you whether you want to go for a rootless setup or not. You probably want to opt for the default root-based installation.

Alternatively, if you do not like piping scripts to your shell, you can apply the manual installation procedure for Ubuntu outlined in the Docker documentation.

Add your user to the Docker group

To enable running Docker commands without sudo, add yourself to the docker group by executing the following commands:

sudo adduser $USER docker
newgrp docker

The second command, newgrp, is there just to add you to the group in the currently opened terminal.

Verify that this worked by running Docker without sudo:

docker run --rm -it docker/whalesay cowsay "All is good"

If you see a whale telling you all is good, you’re not, in fact, going crazy — all is good 🎈.

Make Docker start automatically when WSL starts

WSL distributions lack a process manager for autostarting processes at boot time (or that supports socket activation). This means that the Docker Daemon will not automatically start… unless we make it 💪! We will instruct your Ubuntu instance to execute a command confirming the daemon is started every time we open a shell 🐢.

Start by giving yourself permission to start the daemon without prompting for a password. Execute the following command in your Bash shell:

sudo tee /etc/sudoers.d/90-docker-start \
<<< "$USER ALL=NOPASSWD: /usr/sbin/service docker start" \
> /dev/null \
&& sudo chmod 0440 /etc/sudoers.d/90-docker-start

Then, make Ubuntu confirm the presence of the daemon at every opening of an (interactive) shell by adding a check to your .bashrc file. Do this by executing:

tee -a ~/.bashrc \
<<< "pidof /usr/bin/dockerd > /dev/null || sudo /usr/sbin/service docker start" \
> /dev/null

There! Now, the Docker daemon should automatically start itself 🥳.

Limitations

  • You cannot start Docker containers directly from Windows; interfacing with Docker is possible only from within WSL.
  • You do not have visibility on running containers from Windows or your PC’s tray icon.
  • If you are running many WSL distributions, you will have to run an instance of Docker in each distribution. This contrasts with the Docker Desktop setup, which shares a single Docker daemon across instances.

--

--