How to create a customizable alpine distribution with docker and other utilities to use with wsl

This article will help you to create a customizable alpine distribution that you can use on a normal Windows or a Windows server 2022. It includes how to install docker and other utilities.

Posted: Oct. 14, 2022 • 2 mins read • Author: Adams Pierre David

Table of contents


An alpine with a boat looking like the docker company logo without the whale

Download alpine

First, download the official distribution on

alpine

Choose x86_64 under Mini Root Filesystem or just click on the link below if you want the one versioned 3.16.0:

alpine-minirootfs-3.16.0-x86_64

Step 2

Make sure you already have WSL on your computer to continue. Import the distribution you downloaded:

wsl.exe --import alpine "C:\Path-of-where-you-want-to-install-it" "C:\Path-of-your-downloaded-alpine"

Step 3

Enter the distribution in WSL2

wsl.exe -d alpine

Update and install common utils

apk update --no-cache && apk add --no-cache openrc docker docker-compose nano curl wget

Create a docker folder to put the configuration for docker

mkdir -p /etc/docker

In the folder /etc/docker, create a file named daemon.json

cd /etc/docker && sudo nano daemon.json

and add the following

{
   "builder": { "gc": {"defaultKeepStorage": "20GB", "enabled": true } },
   "experimental": false,
   "features": { "buildkit": true },
   "default-address-pools": [
           {
               "base": "172.240.0.0/16",
               "size": 24
           }
   ]
}

To quit nano: CTRL + s, CTRL + x

Add docker to root group

addgroup root docker

In /etc/wsl.conf add the following

[wsl2]
# bridge Hyper-V vmSwitch inside wsl2
networkingMode = bridged
vmSwitch = Replace-It-With-a-NIC-You-Created
[boot]
# start openrc at boot, doesn't work on Win10, must be Win11 or server 2022
command = "/usr/bin/env -i /usr/bin/unshare --pid --mount-proc --fork --propagation private -- sh -c 'exec /sbin/init'"

In /etc/init.d/ create a file named wait_for_docker without extension (.something) and add the following:

#!/bin/sh
# block until docker starts
FILE=/var/run/docker.sock
while true ; do
    if [ -S "$FILE" ]; then
        break
    fi
done

Add the right to execute the file

chmod +x /etc/init.d/wait_for_docker

Add docker to rc-udpate

rc-update add docker default

Step 4

Remove the command history

rm ~/.ash_history

Exit alpine

exit 

Export the distribution

wsl.exe --export alpine alpine_customized.tar && gzip alpine_customized.tar

Related Articles

Someone in his office in front of his laptop applying machine learning algorithm to an AI model
Person learning via a platform built with AI and ML
Students with backpack trying to learn something in a computer