Install Docker directly in WSL without Docker Desktop
Introduction
I use Windows, mainly because I’m a nerd and I like CAD and 3D printing and I do a bit of gaming then and there; all of these require Windows.
But when it comes to coding, I really like Linux, specially Ubuntu. I’m also used to deploying my services in Linux machines as well.
So for my development workflow I use docker devcontainers, which allow me to develop inside the same environment my app will be deployed in, ensuring a smooth experience and no conflicts.
In windows, there is an issue though, and that is that VSCode Dev Containers really don’t like the native Docker Desktop and the experience is horribly slow and impossible to work with. So you need to install WSL, which stands for Windows Subsystem for Linux, then enable WSL support in Docker Desktop and voila, dev containers working like a charm.
There are some nuances though…
Why I decided to switch
This is really biased, but I’ve experienced multiple nuances with docker desktop, mainly:
- A couple of times a year it would just crash or not start, which got me down the rabbit hole of trying to debug the issue and I would lose a day working on it.
- While pressenting a nice UI, I’m used to the docker CLI, so I don’t use the GUI at all and it consumes unnecessary resources.
- WSL integration within Docker Desktop is quite polished, but still, it would have issues sometimes.
- The hard disk space consumed by Docker Desktop became insane in my Windows system at one point, and after researching, the only way to free up some space was to re-install Docker Desktop from scratch… so I decided to just delete it and move to docker CLI directly in WSL, bypassing the Windows integration.
I’ve found that running docker directly in WSL is smoother and lighter on my laptop’s resources, which, for local development, is so much nicer as my laptop battery lasts more.
In the end, installing Docker directly in WSL is quite easy, specially if you’re used to Docker CLI and Linux, so it was a no brainer.
How to install docker in WSL without installing docker desktop
Let’s get to it:
1. Uninstall any previous Docker installations
If you have Docker Desktop installed, proceed to uninstall it. You can do so by removing the app with the Windows “Remove Apps” utility
2. Install WSL if you don’t have it
WSL is quite easy to install, it’s outside the scope of this article though, so I won’t go into it here.
3. Modify the IP tables installations
If your are NOT on Ubuntu 22.04 or Debian 10 / 11, you can ignore this step
If you, like me, love Ubuntu and have version 22.04 as your WSL distro, you need to modify the IP tables file:
In your WSL terminal run:
sudo update-alternatives --config iptables
Then select option 1to switch to the legacy ip tables installations
4. Install Docker directly in WSL
Depending on your Linux distro there might be some changes to this process, so I advise you to go to the Docker installation docs and select yours.
For Ubuntu though, this is the latest process using apt:
- Install the docker repo
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
- Install Docker using
apt:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
5. Add your user to the Docker group
sudo usermod -aG docker $USER
If the docker group wasn’t created automatically:
sudo addgroup docker
6. Update your DNS
WSL points the DNS server to your local machine in a weird but effective way. This is usually ok but I’ve found that sometimes I would have connectivity issues in my docker containers. So I recommend doing the following:
- Update your nameservers
Edit your resolv.conf file:
sudo nano /etc/resolv.conf
Change the existing nameserver to Cloudflare’s (1.1.1.1) or Google’s (8.8.8.8) DNS:
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
# nameserver 10.255.255.254 -> commented this out
nameserver 1.1.1.1
- Stop
resolv.conffrom regenerating at boot
Edit /etc/wsl.conf:
sudo nano /etc/wsl.conf
Add the following 2 lines to it:
[boot]
systemd=true
# Added these 2 lines:
[network]
generateResolvConf = false
7. Restart your shell to make the new assignments work
You can either:
- Close WSL and start it again
- Run
su - $USERto start a new shell session without closing it
8. Done!
You can test your docker installation by running:
docker run hello-world