How to run docker on Windows without Docker Desktop
Tags: docker, windows, wsl2
Published: 2021-09-26 19:25:09

Since  Docker announced a new subscription for Docker Desktop for personal use, educational institutions, non-commercial open-source projects and small businesses, other enterprises need to acquire licences for all installations of Docker Desktop.

So is there an alternative on Windows to continue to legally use containers with a docker command and a nice UI like VSCode without paying a licence : the answer is YES !

We are doing magic with Windows 10, Ubuntu on WSL2, *docker builder cli* for windows and a little elbow grease.

Big Thanks to Jonathan Bowman for [his article](https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9).

I reused and I adapted it to make *VisualCode* working with **dockerd** under WSL2.

## Installation on Windows

On your windows, you need to install a couple of things :

- WSL2 : [Install WSL | Microsoft Docs](https://docs.microsoft.com/en-us/windows/wsl/install#step-2-update-to-wsl-2)
- Ubuntu on WSL2 : in Microsoft Store Ubuntu 20.04 LTS
- Visual Code : it can be downloaded at [Visual Studio Code - Code Editing. Redefined](https://code.visualstudio.com/)
- Docker extension for VSCode : directly from Visual Code Extensions Marketplace
  ![VSCode Docker extension](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4l24p641gi3paubq2c2.png)

## Installation of dockerd in WSL2/Ubuntu

*(Inspired from the Jonathan Bowman's article)*

### Is your user a "sudoer" ?

Check if **sudo** is installed if not : `*apt install sudo*``

``grep -E 'sudo|wheel' /etc/group``

You would see something like  *sudo: x:27:myusername*

Otherwise, We use **usermod** to add an user to the *sudoer* group
		 
``usermod -aG sudo myusername``

Finally you can check with this command : 

``sudo grep -E '%sudo|%wheel' /etc/sudoers``

You'll have something like

``%wheel ALL=(ALL) ALL``
    
If you see a # at the first position, the line is commented, run **sudo visudo**, find the corresponding line and remove the #, save and check again.

### Update your Ubuntu distro

``sudo apt update && sudo apt upgrade``

### Remove Residue from previous docker installations

``sudo apt remove docker docker-engine docker.io containerd runc``

### Debian/Ubuntu package repository configuration

``source /etc/os-release``

Trust the repo :

``curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo apt-key add -``

Update repo info :

``echo "deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update``

### Install official Docker release ##

``sudo apt install docker-ce docker-ce-cli containerd.io``

### Add user to docker group

``sudo usermod -aG docker $USER``

*"Then close that WSL window, and launch WSL again. You should see docker when you run the command groups to list group memberships."*

## Get IP address in WSL2

``echo \`ifconfig eth0 | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d:\` ``

You should have something like  **172.20.5.64**

## Launch dockerd

In WSL, there is no *systemd* or other init system. So we need to launch manually docker with the automatic collect of the IP address

``sudo dockerd -H  \`ifconfig eth0 | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d:\` ``

There should be several lines of info, warnings related to tls, and the like, with something like API listen on **172.20.5.64:2375** at the end. If so, you have success.

## Test docker command

Get the IP address given with the line API listen and In another WSL terminal, you can test the following command :

``docker -H 172.20.5.64 run --rm hello-world``

You'll get something like this :

>Hello from Docker! 

> ...

## Installing **Docker.exe** on Windows

Stefan Scherer is maintaining the project docker-cli-builder on [GitHub](https://github.com/StefanScherer/docker-cli-builder/releases) where we can download the **docker.exe** command in standalone :

- Download the exe
- Put it in the directory like **c:\bin**
- Add this directory in the path for executables : *System Properties\Environement Variables\System Variables\Path*

![Add path to docker binary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfwpqdkh8xhy2xhy6hwc.png)

### Check if **docker** is working

Once done, logout from your session and log again
In a windows terminal (Windows Power Shell) , launch :

``docker --version``

You would get something like :
		
>Docker version 20.10.5, build 55c4c8896

### Launch dockerd 

Open a terminal in Wsl2, you execute 

``sudo dockerd -H  \`ifconfig eth0 | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 |awk '{ print $2 }' | cut -f2 -d:\` ``

And you get the IP address, as described before

### Test docker on Windows

In the Powershell windows of the terminal, you can run the following command
``c:\bin\docker -H tcp://172.20.5.64 run --rm hello-world``

And you would get :
		
>Hello from Docker!

>...		

*Great we have now docker in windows running with WSL2.*
*But let's continue magic !*

## Configure VSCode to access to WSL2 docker

If you launch Visual Code and you select the docker extension, you'll get error in the panel asking if docker is installed... Yes of course it's installed but not configured to access to WSL2

![VSCode with docker extension errors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4onnmug3igs95alhd9hh.png)

To do so, click on the icon (?) on the top right of the section "Containers" and select "Edit settings..."

![VSCode edit docker settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w88fkywtbd5sm4sf1ota.png)

You'll get around 56 settings and you search for  *"Docker:Host"*  where you put the line **"tcp://172.20.5.64:2375"**  where you can replace the highlighted ip address by the one you got before

![VSCode set Docker:Host](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6b02nk14i4xaielwoxr.png)

Once done, you come back to the panel and you click on *"refresh"* icon (top right of each sections) and you would get information from your **dockerd** running in WSL2

![VSCode it works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehfyeoma7e96xao94nqn.png)

## Making everything works without knowing IP

Now, how to run **dockerd** and *docker* without copy&paste IP address in command line nor VSCode. 

In WSL2, it's not possible to assign IP address but, I can use the windows port forwarding to redirect a local port from the host to a specific one of my distribution. Hence I could put **"tcp://localhost:2375"** in VsCode and the calls will be redirected to dockerd running in WSL2-Ubuntu.

For this, I run the powershell script lines in windows  terminal *running as administrator* :
	

``$ip = (wsl sh -c "hostname -I").Split(" ")[0]``

``netsh interface portproxy add v4tov4 listenport=2375 connectport=2375 connectaddress=$ip``

``wsl sh -c "sudo dockerd -H tcp://$ip"``

Script explanation :	

- First, I collect the IP address of my default distro with the wsl command.
- Second, I set the port forwarding *2375* to my distro
- Third, I launch in my distro dockerd with the IP

When executing these lines you'll be prompted to enter your distro password (*sudo*) and I'll see after the log of dockerd. Everything will work fine when I'll see the message *"API listen on 172.18.75.23:2375"*.

In parallel, in a windows terminal opened in my distro, I can check with **top** or **htop** if *dockerd* processes are running.

In *VSCode*, I update my *Docker:Host* setting with *tcp://localhost:2375* :

![VSCode Docker host to localhost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv8ty56xvrw5vjw9tqsm.png)

And the magic is there :

![VSCode Docker extension working with localhost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/100j4d491bo3f9uq82iq.png)

Now I can know create a dedicated powershell script with the previous line : **start_docker.ps1**

In a windows terminal running with administrator privileges, I set the *Execution policy* with  :

``Set-ExecutionPolicy RemoteSigned``

And every time I want to run **dockerd**, I launch the **start_docker.ps1** script:

![Launch docked from Windows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ft5cjuok5l546a715uz.png)

And if you see *API Listen on 172.18.75.23:2375* 

Everything works !

![Logs 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f0xyu8rxvhu13bwai0z.png)

Now, I want to use **docker** without *-H* parameter, for this, I add a new system environment variable called *DOCKER_HOST* set to **tcp://localhost:2375**

![Add DOCKER_HOST as system variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1dwdxjebmy8j4d108rg.png)

Finally, in a windows terminal, I can simply run a command like this:

``docker image ls``

![Simple use of docker command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oplxg2wm78f4lg5gevrz.png)

## In conclusion

This article shows how we can use docker in windows and WSL2 without Docker Workstation
To do so, we just need first to run a powershell script launching **dockerd** in WSL2 and once **dockerd** is listening we can simply use the command **docker** (maintained by Stefan Scherer).

Yes ! We can continue to develop with containers without Docker Workstation.

Enjoy !