Skip to content

Container engine

A container engine (based on WSL2) allows to run software in an isolated environment. I recommend either Docker or Podman.

Since I am using Podman, all commands my examples will start with podman. If you are using Docker, just replace this command with docker - the rest should work the same.

Shrink ext4.vhdx

All volumes not mapped to a host folder are stored in a .vhdx file.

Location of ext4.vhdx

With Podman the (default) location is %USERPROFILE%\.local\share\containers\podman\machine\wsl\wsldist\podman-machine-default\ext4.vhdx. With Docker it is %LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx.

Since diskspace is not released when files are deleted from volumes, this file can get very large. You can use diskpart (as described on Stack Overflow) to shrink the file to its actual size:

  1. Stop all WSL instances: wsl --shutdown
  2. Verify all instances are stopped: wsl --list --verbose
  3. Start diskpart
    1. Select the .vhdx file with select vdisk file="" (for Podman: DISKPART> select vdisk file="%USERPROFILE%\.local\share\containers\podman\machine\wsl\wsldist\podman-machine-default\ext4.vhdx")
    2. Shrink the file: DISKPART> compact vdisk
    3. Close the window : DISKPART> exit

Start the Podman Machine for a scripted task

Sometimes you want to start the Podman Machine in order to execute a task and end it, once the task completes. However, you don't want to stop the machine (and potentially other containers) if it was already running.

@echo off
rem Check if Podman machine is running
podman machine list --format "{{.LastUp}}" | findstr /C:"Currently running" > nul
if %ERRORLEVEL% neq 0 (
    echo Podman machine is not running. Starting the machine...
    podman machine start

    echo Executing task...
    rem YOUR COMMAND HERE

    echo Stopping Podman machine...
    podman machine stop
) else (
    echo Podman machine is already running. Executing task...
    rem YOUR COMMAND HERE
)

Wait for the Docker Engine

Start Docker Desktop and wait until the Docker Engine is ready (source).

@echo off
"%ProgramFiles%\Docker\Docker\Docker Desktop.exe"
docker-compose ls >nul 2>nul
if %errorlevel%==0 goto :EOF
echo Waiting for Docker Engine to start . . .
:waitloop
timeout /t 10
docker-compose ls >nul 2>nul
if %errorlevel%==1 goto waitloop