Memory allocation to docker containers after moving to WSL 2 in Windows
Asked Answered
C

3

59

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?

Catafalque answered 16/6, 2020 at 9:54 Comment(2)
Did this command work for a specific container docker run -d -p 8081:80 --memory="256m" container_name?Smukler
Yes it did @SmuklerCatafalque
E
100

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.

Extremadura answered 7/7, 2020 at 10:52 Comment(14)
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.Catafalque
Absolutely @MurtazaHaji, that's correct, it is the overall WSL memory consumption. Have you tried to apply the limitations show here (docs.docker.com/config/containers/resource_constraints)? I haven't, but I will try and come back to you!Extremadura
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.Catafalque
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.Richmound
Restart is not necessary, just run "Get-Service LxssManager | Restart-Service" in your admin powershellCalvo
@Smukler 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.Confab
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.Parka
See doc : learn.microsoft.com/en-us/windows/wsl/…Retinoscopy
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?Frechette
@Frechette 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 learn.microsoft.com/en-us/windows/wsl/wsl-configExtremadura
@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! :-)Frechette
@Frechette 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.<service-identifier>.deploy.resources.limits.memory. Details here: docs.docker.com/compose/compose-file/compose-file-v3/#resourcesExtremadura
@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?Frechette
@Frechette I limited my WSL with 4GB in the .wslconfig, indeed.Extremadura
M
16

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


Experimental Solution (September 2023 Update)

This September 2023 update added support for new opt-in experimental features, including the new autoMemoryReclaim which makes the WSL VM shrink in memory as you use it by reclaiming cached memory. If you want to try it...

Add this new experimental section to your %UserProfile%\.wslconfig file:

[experimental] 
autoMemoryReclaim=gradual

See this and more experimental settings here: Windows Subsystem for Linux September 2023 update

Mohsen answered 16/11, 2022 at 4:0 Comment(0)
E
0

Make sure GB is uppercase.

If Alexz answer does not work, your .wslconfig file may be invalid, and the event viewer may contain a warning with a description of what is wrong.

Go to Windows > Event viewer > Windows logs > Application, and find some entries in the list that are warnings or errors, and where the Source column is WSL.

I had a warning which said (translated):
Invalid memory string 10gb for .wslconfig entry wsl2.memory in C:\Users\Username\.wslconfig:3

Changing the memory setting in .wslconfig to "10GB" fixed the problem. In my case

screenshot of event viewer with error message highlighted

Europium answered 24/1 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.