Docker on WSL2


How to run docker on Win10 using WSL2 and troubleshooting common problems.

Published on March 01, 2022 by Tony E.

docker WSL WSL2

3 min READ

For a project I was doing (building a custom VyOS ISO) I wanted to try running a container using Docker in my WSL environment on my laptop. I was told this works and read many examples on how to achieve this.

While, I do fancy myself as someone who pays close attention to detail, this time I was bested.

I followed many guides to get docker running in WSL.

I’m really loving having native linux on my Windows 10 laptop. There’s a small learning curve when needing to troubleshoot if things go wrong.

Not being a docker expert myself or experienced with Windows Sub-system for Linux (WSL), I was following someone else’s guide. No where was it mentioned about the different versions of WSL or any problems that could cause.

Exhibit: A

https://dev.to/felipecrs/simply-run-docker-on-wsl2-3o8

# Ensures not older packages are installed:
sudo apt-get remove docker docker-engine docker.io containerd runc

sudo apt-get update

# Ensure pre-requisites are installed:
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

# Adds docker apt repository:
echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Adds docker apt key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
    sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Refreshes apt repos:
sudo apt-get update

# Installs Docker CE:
sudo apt-get install docker-ce docker-ce-cli containerd.io


# Ensures docker group exists:
$ sudo groupadd docker

# Ensures you are part of it:
$ sudo usermod -aG docker $USER

# Close your shell and reopen to make sure you're in the correct group (docker):
groups

# Open ~/.profile and add:
if service docker status 2>&1 | grep -q "is not running"; then
    wsl.exe -d "${WSL_DISTRO_NAME}" -u root -e /usr/sbin/service docker start >/dev/null 2>&1
fi

Even after manually starting the service (sudo service docker start), I would run the ‘hello-world’ test and get an error:

user@hostname:~$ docker version

Client: Docker Engine - Community
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:37:06 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

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

The above error along with others was indicating iptables couldn’t be found. Even when I manually check for iptables, I found it’s not installed! How could this be? Now, were shifting from running and troubleshooting docker to investigation why iptables isn’t included in Ubuntu.

Turns out, iptables isn’t included with Ubuntu distro using WSL version 1. Unknowingly, I was using WSL1 not WSL2.

You can check that in a Windows command prompt:

wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         1

You can set a version number by:

wsl --set-version ubuntu 2

When I did this, I received an error from Microsoft about a “kernel update package” and a hyperlink: https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4—download-the-linux-kernel-update-package

You’ll need to install the Kernel Update Package as administrator.

Then, set the version of WSL for your distro:

wsl --set-version ubuntu 2

It takes a few minutes to convert, but once it does, I was able to open ubuntu and the iptables command worked perfectly as well as starting the Docker daemon.

I’ve set my default WSL version to “2” using: wsl --set-default-version 2

I am now a WSL lover and trying to incorporate it in many of my workflows.