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

Viewed 37600

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?

1 Answers

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 tweaking 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.

Related