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

Murtaza Haji picture Murtaza Haji · Jun 16, 2020 · Viewed 8.4k times · Source

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?

Answer

Alexz picture Alexz · Jul 7, 2020

The Memory and CPU settings are removed for WSL2 integration. However, as part of Windows Build 18945, there is a workaround to limit WSL2 memory usage.

  • Introduce %UserProfile%\.wslconfig file for tweaking WSL2 settings

Microsoft WSL team introduced a file called .wslconfig for configuring WSL2 settings.

You basically have to create that file at %UserProfile%\.wslconfig and set the settings below.

[wsl2]
memory=6GB  # Any size you feel like
swap=0
localhostForwarding=true

Restart your computer, and from now on, you won't have any problem with high memory consumption.

Hopefully, it helps!