dockerdocker-for-windowsubuntu-20.04wsl-2

Memory allocation to docker containers after moving to WSL 2 in Windows


I recently updated my Docker environment to run on WSL 2 on Windows.

For setting memory allocation limits on containers in previous versions, I had option in Docker Desktop GUI under Settings->Resources->Advanced->Preferences to adjust memory and CPU allocation.

After WSL 2 integration, I am not able to find that option. enter image description here

I assume I should run everything through my Linux distro from now on, so this is the solution I was able to find:

docker run -d -p 8081:80 --memory="256m" container_name

I dont want to have to set a flag each time when running a container. Is there a way to permanently set the memory allocation?


Solution

  • The Memory and CPU settings were removed for WSL2 integration. However, starting in Windows Build 18945, there is a workaround to limit WSL2 memory usage.

    Create a %UserProfile%\.wslconfig file for configuring WSL2 settings:

    [wsl2]
    memory=6GB  # Any size you feel like (must be an integer!)
    swap=0
    localhostForwarding=true
    

    Run Get-Service LxssManager | Restart-Service in an admin Powershell (or reboot) and verify that the vmmem usage in Task Manager drops off.

    For the complete list of settings, please visit Advanced settings configuration in WSL.