Running Docker on WSL2 without Docker Desktop
This is a straight to the point guide on how to make Docker CE run fully on WSL2.
What you will get
- A full-fledged Docker installation on WSL2
- Docker Daemon automatic start without any crazy hacks
What you will not get
- Docker Daemon sharing between Windows and WSL (i.e. you cannot run docker from Windows PowerShell)
- Docker Daemon sharing between WSL distributions
Prerequisites
I will consider that you already have WSL2 working, and you are using Ubuntu as your distribution. Guide
Install Docker CE on Ubuntu by following the official guide
Ensures not older packages are installed
sudo apt-get remove docker docker-engine docker.io containerd runc
Ensure pre-requisites are installed
sudo apt-get update 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
Perform the post-installation steps
Ensures docker group exists
sudo groupadd docker
Ensures you are part of it
sudo usermod -aG docker $USER
Now, close your shell and open another for taking the group changes into account If you are running Ubuntu 22.04, you will also need to run the following command:
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
If you are running Windows 11, you can use a brand-new feature of WSL to start the Docker Daemon during the initialization. You only need to add:
[boot]
command = "service docker start"
To your /etc/wsl.conf within your WSL distribution. Then, restart it with wsl.exe --shutdown. To verify that it works, you can run docker version. If you do not receive any permission denied error, you are good.
Source: https://dev.to/felipecrs/simply-run-docker-on-wsl2-3o8