Docker under WSL2 - crackodeal/Learning GitHub Wiki

How to use Docker Engine with no Docker Desktop on Windows

Why this can be needed?

I had a case when the commercial license was pending on the organization but the work must be done before license in acquired.

Solution

Install only Docker Engine and Docker CLI under WSL2

Install WSL2 - use Ubuntu 20.04

in PowerShell wsl --install make sure WSL2 installed wsl --upgrade use Ubuntu 20.04 by default

Install Docker in Ubuntu instance

Install docker-compose on Windows

Installation

a. Install Docker Install Docker in your WSL2 Ubuntu instance:

sudo apt update sudo apt install -y docker.io

Add your user to the docker group to avoid using sudo:

sudo usermod -aG docker $USER newgrp docker

Config notes

Docker environment

image

Auto start

WSL2 does not support systemd natively, so services don't start automatically as they would on a full Ubuntu installation. Use the following workaround:

Create a script file to start the Docker service:

sudo nano /usr/local/bin/start-docker.sh

Add the following content:

#!/bin/bash sudo service docker start

Save the file and make it executable:

sudo chmod +x /usr/local/bin/start-docker.sh

Modify ~/.bashrc or ~/.zshrc:

Open your shell configuration file:

nano ~/.bashrc

Add this line at the end of the file:

if [ -z "$(pgrep dockerd)" ]; then sudo /usr/local/bin/start-docker.sh fi

Save and reload the shell configuration:

source ~/.bashrc

Docker login

docker login

I gave up with credential managers because I could not make it work so I emptied ~/.docker/config.json (leave only "{}" as the empty json content) Do the login and Docker will store encrypted data in the config file

Usage notes

Ubuntu shell

It is practical for configuring/starting/stopping docker as service. Set it to start automatically https://chatgpt.com/c/67506085-cdd4-8011-8789-cbc6240bc566

Windows Terminal

It is practical for building images

'wsl docker [command]' You can talk to Docker from Windows Terminal (combination of PowerShell with Unix shell) by preceding commands with 'wsl' This is needed, for example, if you need to build Dockerfile from your current windows path wsl docker build --no-cache -t myimage:latest . The new image will appear in your Ubuntu Docker 'wsl docker image ls'