Docker in WSL 2
Installing Docker in a Linux machine is very straightforward. Docker documentations is pretty helpful and makes it easy to install it in the most common Linux distributions. But what about Windows? Well, it’s a bit more complicated, specially if you want to use WSL 2.
You can install Docker Desktop for Windows, and it’s the easiest way to get Docker running on Windows, but it’s not the best way. Docker Desktop for Windows is a resource hog, and it’s not very stable, in my experience. In addition, Docker made some licensing changes that make it harder to use Docker Desktop for Windows in a corporate environment.
When researching how to install Docker in WSL 2, I found a lot of different ways to do it. To spare you the trouble of hacking around the terminal, I’ll show you my way of installing Docker in WSL 2.
Note: To follow this guide, it is assumed that you already have WSL 2 enabled and installed in Windows.
In a powershell window, run the following commands to install Ubuntu, enabling it to run on WSL 2 and turning on the Virtual Machine Platform feature:
wsl --install -d Ubuntu
wsl --set-version Ubuntu 2
wsl --update
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Restart the computer, just to be sure. Open the Ubuntu terminal and check for updates:
sudo apt update
sudo apt upgrade -y
If you already have Docker installed, uninstall it:
sudo apt-get remove docker docker-engine docker.io containerd runc -y
Note: If it is a fresh install of Ubuntu, you can skip this step.
Install some dependencies:
sudo apt-get install ca-certificates curl gnupg lsb-release -y
Add the GPG key and the Docker repository:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg --batch --yes
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
Update the apt index and install Docker:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Add your user to the docker group:
sudo usermod -aG docker $USER
Note: You need to log out and log back in for this to take effect.
If you also want to install Docker-Compose, run the following commands:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s --force /usr/local/bin/docker-compose /usr/bin/docker-compose
Note: You can change the version of docker-compose by changing the version in the URL.
To enable Docker to start when you open the terminal, run the following command:
sudo systemctl enable docker
Change the default iptables to iptables-legacy:
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
Close the terminal and open it again. You should see a message that Docker is running.
To confirm that everything is OK, run the following command:
docker run hello-world