50

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?

Murtaza Haji
  • 1,093
  • 1
  • 13
  • 32

2 Answers2

89

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.

Alexz S.
  • 2,366
  • 4
  • 21
  • 34
  • 5
    Yes, I did this. Infact I pushed the Microsoft documentation team to improve docs regarding the `.wslconfig` file. There is an ongoing issue where `Vmmem` takes up large chunk of memory, they plan to resolve it soon. This solution only addresses the memory allocated to Linux subsystem. I was looking for more granular control over each containers resources. Thanks for the answer though. Ill wait for a few more days for appropriate answer. – Murtaza Haji Jul 07 '20 at 21:56
  • 1
    Absolutely @MurtazaHaji, that's correct, it is the overall WSL memory consumption. Have you tried to apply the limitations show here (https://docs.docker.com/config/containers/resource_constraints/)? I haven't, but I will try and come back to you! – Alexz S. Jul 08 '20 at 13:12
  • 1
    Yes , the memory flag address this issue on per container basis, maybe there could be a permanent flag for assigning memory, cant find that option though. – Murtaza Haji Jul 08 '20 at 17:15
  • 1
    While this answer may not answer the question, it does relate very well to the title of the question. I.e., it works for googlers like me. – Culme Feb 12 '21 at 11:22
  • 5
    Restart is not necessary, just run "Get-Service LxssManager | Restart-Service" in your admin powershell – Ilendir Mar 04 '21 at 13:24
  • 2
    @srk the answer is indeed relevant to Docker for those who are running Docker Desktop for Windows using the WSL2 back end, because then the Docker daemon is running in WSL. – Stabledog Jul 22 '21 at 11:32
  • 12
    The ".wslconfig" file does not support floating-point numbers. If you set `memory=2.5GB`, it just ignores your settings, without any warning. Use integers. – Mohammad Dehghan Jul 29 '21 at 22:36
  • 1
    See doc : https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configuration-setting-for-wslconfig – Guillaume Husta Jan 24 '23 at 09:45
  • Maybe a stupid question: is this necessary if you wish to increase the memory allocated to WSL? Or is it only useful to limit the memory allocated? – TrojanName Feb 28 '23 at 13:00
  • 1
    @TrojanName it's the amount you want to assign and limit to WSL 2 VM, but it has constraints as quoted "50% of total memory on Windows or 8GB, whichever is less; on builds before 20175: 80% of your total memory on Windows". More details at https://learn.microsoft.com/en-us/windows/wsl/wsl-config – Alexz S. Feb 28 '23 at 13:18
  • @AlexzS.thanks. So my reading of that is that if I want to increase the allocated memory, I don't need to set this limit in .wslconfig. My problem is that my container doesn't have enough memory, so I definitely don't want to limit it! :-) – TrojanName Feb 28 '23 at 13:38
  • 2
    @TrojanName make sure your WSL 2 VM has enough memory allocated. I allocate 4GB in mine. Also, if it's your container not having enough memory, it can be your Docker compose configuration (in case you have one). Make sure you're not setting a resource limit. It's under `services..deploy.resources.limits.memory`. Details here: https://docs.docker.com/compose/compose-file/compose-file-v3/#resources – Alexz S. Feb 28 '23 at 15:11
  • @AlexzS. oh thanks very much for this! It's actually during the docker build step that I'm hitting the limits, so right now it's failing to build. When you say you allocate 4GB, do you mean that you are doing that in this .wslconfig, or some other way? – TrojanName Feb 28 '23 at 15:14
  • 1
    @TrojanName I limited my WSL with 4GB in the .wslconfig, indeed. – Alexz S. Feb 28 '23 at 15:58
8

You must limit WSL memory usage...

Step 1

Add/Edit this file %UserProfile%\.wslconfig and append these two lines:

[wsl2]
memory=8GB

Step 2

Do a full shutdown right after for WSL to pick up the new settings:

$ wsl --shutdown

See additional information from Microsoft here: Advanced settings configuration in WSL

Jorge Garcia
  • 2,042
  • 23
  • 25