If you run docker desktop with the new WSL 2 backend and enable Use the WSL 2 based engine
in Settings/General :
show settings/general
when you want to modify config settings for WSL 2 to explicitly limit the available resources you can do that by creating a .wslconfig file inside your user profile directory
Open Windows Terminal/CMD/PowerShell and run the commands below:
# turn off all wsl instances such as docker-desktop
wsl -- shutdown
notepad "$env:USERPROFILE/.wslconfig"
then add this items in .wslconfig file :
[wsl2]
kernel=<path> # An absolute Windows path to a custom Linux kernel.
memory=<size> # How much memory to assign to the WSL2 VM.
processors=<number> # How many processors to assign to the WSL2 VM.
swap=<size> # How much swap space to add to the WSL2 VM. 0 for no swap file.
swapFile=<path> # An absolute Windows path to the swap vhd.
localhostForwarding=<bool> # Boolean specifying if ports bound to wildcard or localhost in the WSL2 VM should be connectable from the host via localhost:port (default true).
# <path> entries must be absolute Windows paths with escaped backslashes, for example C:\\Users\\adamc\\kernel
# <size> entries must be size followed by unit, for example 8GB or 512MB
For Example :
[wsl2]
memory=2GB
processors=4
Then Save the file and restart the docker-desktop service.