This content originally appeared on DEV Community and was authored by Nicolas Louis
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.
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
- Ubuntu on WSL2 : in Microsoft Store Ubuntu 20.04 LTS
- Visual Code : it can be downloaded at Visual Studio Code - Code Editing. Redefined
- Docker extension for VSCode : directly from Visual Code Extensions Marketplace
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 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
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
To do so, click on the icon (?) on the top right of the section "Containers" and select "Edit settings..."
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
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
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 :
And the magic is there :
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:
And if you see API Listen on 172.18.75.23:2375
Everything works !
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
Finally, in a windows terminal, I can simply run a command like this:
docker image ls
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 !
This content originally appeared on DEV Community and was authored by Nicolas Louis
Nicolas Louis | Sciencx (2021-09-26T19:25:09+00:00) How to run docker on Windows without Docker Desktop. Retrieved from https://www.scien.cx/2021/09/26/how-to-run-docker-on-windows-without-docker-desktop/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.