Skip to content

Instantly share code, notes, and snippets.

@K0IN
Last active September 13, 2024 20:47
Show Gist options
  • Save K0IN/485b6c9e442d10aa666c28672d621604 to your computer and use it in GitHub Desktop.
Save K0IN/485b6c9e442d10aa666c28672d621604 to your computer and use it in GitHub Desktop.
Build a wsl distro using docker

How to use

run the bat

docker-to-wsl.bat

then you can use the new distro (named "distro")

wsl -d distro

Entrypoint

if you want a custom entrypoint (on wsl start) you can use /etc/wsl.conf to set a custom entry command

Example

RUN printf "[boot]\ncommand = /etc/entrypoint.sh" >> /etc/wsl.conf
RUN printf "#!/bin/sh\ntouch /root/booted" >> /etc/entrypoint.sh
RUN chmod +x /etc/entrypoint.sh
docker build -t tmp-img .
if %errorlevel% neq 0 exit /b %errorlevel%
for /f "delims=" %%i in ('docker create tmp-img') do set CONTAINER_ID=%%i
docker export %CONTAINER_ID% --output="./latest.tar"
if %errorlevel% neq 0 exit /b %errorlevel%
docker rm tmp-img
call wsl --unregister distro
wsl --import distro . latest.tar
if %errorlevel% neq 0 exit /b %errorlevel%
call del latest.tar
# you can also change the image.
FROM alpine:latest
# example: install fish shell
RUN apk update && apk add fish shadow
RUN chsh -s /usr/bin/fish
# example on how to set a env variable
RUN fish -c "set -Ux key value"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment