Setup WSL2 for local docker development on Windows

MediumP
3 min readSep 7, 2023

--

Hi, does your work require you to have docker and manage it?

Are you on a Windows machine, but can’t acquire a license for Docker Desktop or similar software either because you’re limited by your company security policies, lack of money, or just don’t want to add more stuff to your machine?

This article is for you as I will walk you through step by step how to enable your local machine for docker development.

To achieve this we will be using WSL2 (Windows sub-system for Linux) that’s available as a feature in Windows to enable developers to run a Linux environment.

Setting up the host machine

Installing WSL2

Navigate on Windows to %UserProfile% in Windows Explorer

Add, .wslconfig document (note: DOT (.) is in the front → .wslconfig)

# Settings apply across all Linux distros running on WSL 2
# Can see memory in wsl2 with "free -m"
# Goes in windows home directory as .wslconfig
[wsl2]

# Limits VM memory to use no more than 48 GB, defaults to 50% of ram
memory=4GB

# Sets the VM to use 8 virtual processors
processors=8

# Sets the amount of swap storage space to 8GB, default is 25% of available RAM
swap=1GB

Open the command prompt and execute the following commands:

Choose Linux distro

wsl --list --online

Install Linux distro

wsl --install -d Ubuntu-20.04

Verify installation

wsl --list --verbose

Upgrade to version 2

wsl --set-default-version 2 wsl --set-version Ubuntu-20.04 2

Verify the version is 2

wsl --list --verbose

Restart WSL

wsl --shutdown

Open Bash shell (Ubuntu) Windows start → search for Ubuntu.

Open the application and move to install the docker engine.

You will be prompted to set your root username and password

Installing docker-engine

Once you are logged in to your Linux distro

Add user as sudo (administrator)

grep -E 'sudo|wheel' /etc/group

Check:

sudo grep -E '%sudo|%wheel' /etc/sudoers

You should see: %sudo ALL=(ALL:ALL) ALL

Update your Ubuntu distro (type Y when asked)

sudo apt update && sudo apt upgrade

Remove previous docker installation residue

sudo apt remove docker docker-engine docker.io containerd runc

Install docker using the repository

Update apt and install packages to allow apt to use a repository over HTTPS

sudo apt-get update

sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release

Add docker official GPG key

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

Set up a stable 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

Install docker-engine

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Add the user to the docker group

sudo groupadd docker
sudo usermod -aG docker $USER

Run docker service

sudo service docker start

Check status (Must be “docker is running”)

sudo service docker status

Install docker-compose (the latest at the time of writing is 2.5.0)

sudo curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Grant execution privileges

sudo chmod +x /usr/local/bin/docker-compose

Check version

docker-compose --version

Output: docker-compose version 1.27.4, build 40524192

Now you can use docker through CMD / Powershell by preceding all commands with “wsl”

Like:

wsl docker image ls

Expose docker to the local machine

Edit daemon.json located at etc/docker/daemon.json

Write "hosts": [ "unix:///var/run/docker.sock","tcp://0.0.0.0:2375"] in it and save.

Restart the machine and start docker with dockerd

Observe the logs to see the hosts from the picture above

Good job, you have set up and enabled your local environment to be productive using docker and docker-compose :)!

References:
https://learn.microsoft.com/en-us/windows/wsl/install
https://dev.to/_nicolas_louis_/how-to-run-docker-on-windows-without-docker-desktop-hik
https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9
https://medium.com/geekculture/run-docker-in-windows-10-11-wsl-without-docker-desktop-a2a7eb90556d

--

--