Publish Date: February 28, 2023

Install Docker in WSL2 as an alternative of Docker Desktop

Developers who use Windows as their primary operating system should know how slow, buggy, and unstable Docker Desktop is. There was no official way of running Docker in Windows except Docker Desktop. Due to this, some people tend to run Linux in a virtual machine, which again is quite resource intensive and hard to maintain. Also, Docker Desktop requires a license for commercial use so if you’re looking for a free and open-source alternative, check out Podman Desktop.

Today we are going to learn how you can run Docker in WSL 2 as an alternative of Docker Desktop. For this, we will install Ubuntu in WSL 2 and then install Docker in it. If you’re new to WSL 2, checkout my previous article, where I discussed about WSL 2 in Windows.

Installing and updating WSL 2

Depending upon the version of Windows you are running, checkout these links to install WSL 2:

In case you have already installed WSL 2, I would recommend you run the following command:

wsl --update

Updating WSL2 in Windows

This command will update WSL 2 in your system and hopefully you will now be running a Microsoft store version of WSL 2 which offers better updates and new features. If you see version 0.67.6 (or above) while running wsl --version command as shown in the screenshot above, you are all set.

Installing Ubuntu in WSL 2

To install Ubuntu in WSL container, run the following command:

wsl --install Ubuntu

Installing Ubuntu Linux in WSL 2

You will see a new window popping up where you need to set your username and password to use with Ubuntu. Once you do this, you are ready to install Docker.

Installing Docker in WSL 2

Now run wsl --distribution Ubuntu command to connect to your Ubuntu instance running in WSL2. Once connected, install Docker in WSL 2 with following commands:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Installing Docker in WSL 2

These commands will download a script from official website and run it to install the latest stable release of Docker in Ubuntu. That’s it. You just installed Docker in Ubuntu running in WSL 2.

Start Docker service on boot

If you have already used Ubuntu Linux before, you will most likely try to run sudo systemctl start docker command to start docker service and sudo systemctl enable docker command to configure it to start at boot. But you will get an error saying System has not been booted with systemd as init system (PID 1). Can’t operate. Failed to connect to bus: Host is down. See the screenshot of error for reference.

System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down

The error is straightforward. It is telling you that Ubuntu distribution in WSL 2 wasn’t booted with systemd init system. So now you have two options:

  • either enable systemd in WSL 2.
  • or use another way for starting and enabling docker service on boot.

Now the first option is quite easy if you are running updated Microsoft store version of WSL2. If this is the case, you just need to create or edit /etc/wsl.conf file with sudo nano /etc/wsl.conf command in your Ubuntu distribution. And then add the following lines to it:

[boot]
systemd = true

Enabling systemd init system in WSL2

By adding these lines, you’re essentially telling your Ubuntu distro to boot with systemd init system. After doing this, you need to terminate your Ubuntu instance either using wsl --terminate Ubuntu command or wsl --shutdown command.

Now when you launch WSL instance once again, you will be able to use systemctl commands and other features of systemd init system.

Starting and enabling docker service on boot with systemd

Alternatively, if you do not want to (or can not) use systemd due to any reason, you could add the following lines in /etc/wsl.conf file instead:

[boot]
command = "/usr/sbin/service docker start"

Start docker service on boot with systemd option in wsl.conf file

Once you restart your Ubuntu distribution, you will notice that docker service runs on boot without systemd. See the following screenshot for reference:

Start docker service on boot without systemd in wsl.conf file

There are other ways of starting docker service on boot but I have showed the most simple ones. That was all for this post. I hope you can now start using Docker in WSL 2 instead of using Docker Desktop.



Microsoft Certified | Cisco Certified

Leave a Reply