How to Run Docker on Windows Using WSL2: A Guide Without Docker Desktop

There are two approaches to install Docker on Windows 10 and 11: through the Docker Desktop for Windows application, which leverages Hyper-V and Windows Containers, or by installing the Docker Engine directly on a Linux distribution within the Windows Subsystem for Linux (WSL2). This tutorial focuses on setting up Docker Engine on a WSL environment, bypassing the Docker Desktop application.

Benefits of opting for Docker Engine on WSL include:

  • No requirement for Windows 10/11 Pro or Enterprise editions as even Windows Home can support Docker Engine in WSL.
  • Docker Desktop consumes additional RAM and disk space for running Hyper-V virtual machines and Windows containers.
  • While Docker Desktop is free for use in small businesses (up to 250 employees), personal use, and non-commercial projects, commercial use outside these parameters requires a paid subscription.

Assuming a functioning WSL2 environment is already set up on your PC, with a Linux distribution such as Ubuntu:22.04 installed, you can start by listing all the WSL distributions available on your system:

wsl --list

To set the default WSL image, run the command:

wsl --setdefault Ubuntu-24.04

Ensure you are on WSL2 by checking the version:

wsl --version

If not, execute the following:

wsl --set-default-version 2

To access your Linux image within the WSL environment, use:

wsl.exe

Update packages in your Linux distribution, specifically Ubuntu:

$ sudo apt-get update && sudo apt-get upgrade -y

Next, proceed to install the Docker Engine on Linux leveraging the official script:

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

Dismiss the warning regarding installing Docker in a WSL environment.

Add your user to the docker group. This allows you to run the Docker commands without having to elevate privileges with sudo or entering a password.

$ sudo usermod -aG docker $USER

Verify that Docker Engine and Compose have been successfully installed:

$ docker --version
$ docker compose version

Now configure the Docker daemon to start automatically in WSL. WSL currently fully supports systemd, the init system and service manager used by many popular Linux distributions. Verify that it is enabled in your WSL Linux image:

$ cat /etc/wsl.conf

[boot]

systemd=true

Then verify that systemd is being utilized as the Linux init system:

$ stat /sbin/init

In this case, systemd is used for init (because the /sbin/init is a symbolic link to /lib/systemd/systemd).

Run the Docker Engine service and enable autostart:

$ sudo systemctl enable --now docker.service
$ systemctl status docker.service

If for some reason systemd is not used, you can add it to the WSL startup:

$ nano /etc/wsl.conf

[boot]

command = "/usr/sbin/service docker start"

The following host computer resources are available to WSL2 by default:

  • 50% of RAM
  • 25% of swap file
  • 100% of the CPU resources

If you want to restrict the use of host resources by the WSL2 subsystem, create a text file %UserProfile%.wslconfig in the current user’s profile. This file can be used to set global restrictions for all WSL distros. For example, add the following:

[wsl2]

memory=8GB

processors=4

swap=2GB

Restart the WSL image from the Windows command prompt:

wsl --shutdown

Run a test Docker container in WSL:

$ docker run hello-world

Docker Engine will download and run the hello-world demo container from the Docker Hub.

To get the best performance from WSL2, store all your Docker container files inside WSL, rather than in directories that are redirected from the Windows host (such as /mnt/c).

Modern Linux versions (Ubuntu 22.04, Debian 10+) utilize nftables instead of iptables for managing the built-in firewall. The Linux kernel 5.8 or later is required for native nftables support in WSL. To ensure proper networking functionality with older kernels, the following command can be used to enable compatibility with iptables:

$ sudo update-alternatives --config iptables

Press 1 to select iptables-legacy mode.

You can now run Docker commands in WSL from the Windows command prompt.

Before any Docker command, you must specify wsl. For example, to list available Docker images, use:

wsl docker images


Posted

in

, ,

by

Tags: