How to Create Docker Images in Windows without Docker Desktop using WSL2


Most people are aware that Docker Desktop is not available anymore for Windows users for free, and this became a challenge. For a couple of months, I thought if there was an alternative for my team, and I came up with a solution using WSL2 and Ubuntu.

To get started you must complete two requirements:

1. Install Windows Subsystem for Linux 2 (it doesn't work with WSL1):

Install WSL | Microsoft Docs

2. Download Ubuntu from the Microsoft Store:

Get Ubuntu - Microsoft Store

If you don't have access to the Store, you can check this tutorial and create your own distro:

Export and Import WSL Linux Distro in Windows 10

After you have installed Ubuntu and opened it for the first time, you can check in PowerShell to see if the version is correct with this command:

wsl -l -v

And you must get something like this:

If it's incorrect, you can check this tutorial and upgrade your Kernel to version 2:

Manual installation steps for older versions of WSL | Microsoft Docs

And later run this command:

wsl --set version Ubuntu 2

After everything is ready, you must run the following scripts in Ubuntu:

First part:

sudo apt-get update

Second part:

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o
/usr/share/keyrings/docker-archive-keyring.gpg

echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg]
https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >
/dev/null

Third part:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

sudo apt-get install docker-compose

Fourth part:

Run this line to start your Docker every time you need it.

sudo dockerd

And that's all! Just open a new Ubuntu window and start playing with Docker! 

As a bonus tip, you can download the Windows Terminal for managing multiple tabs:

Get Windows Terminal - Microsoft Store

NOTES:

1. If you have any issue with the network, check the following location and edit its nameserver IP to 8.8.8.8:

sudo nano /etc/resolv.conf


And to prevent the file is overwritten every time, you can modify this location:

sudo nano /etc/wsl.conf

And add these lines:

[network] generateResolvConf = false

2. This solution only works inside Ubuntu in WSL2; you cannot use it in the Command Prompt or PowerShell. If you must use any of them or create Windows Containers, you are going to need a copy of Docker Desktop and acquire its proper license.

Comments