Docker windows container memory limit

65,801

Solution 1

According to talks on Docker for windows Github issues (https://github.com/moby/moby/issues/31604), when Docker for Windows is run under Windows 10, it is actually using a Hyper-V isolation model (and process model is not accessible in Win 10 scenario).

And in that isolation type, your container is run inside a lightweight VM, which DOES have a default limit, and it is 1 Gb. So if you want to have more memory you should use -m param.

Solution 2

If using a Linux container

For me, on Windows 10 using Docker Desktop, I could not get the --memory= and --cpus= options to work. Here's what does work:

  1. Right click on the Docker whale in the system tray and choose "Settings"
  2. Go to "Resources -> Advanced" on the left
  3. Set how many CPUs and memory is available to containers here.

enter image description here

Solution 3

Surprise! Surprise!

I have 32Gb RAM on my host but I can see only 1Gb RAM given to Windows containers:

D:\>systeminfo | findstr "Memory"
Total Physical Memory:     1,023 MB
Available Physical Memory: 634 MB
Virtual Memory: Max Size:  1,023 MB
Virtual Memory: Available: 674 MB
Virtual Memory: In Use:    349 MB

D:\>wmic OS get FreePhysicalMemory /Value
FreePhysicalMemory=648340

D:\>wmic computersystem get TotalPhysicalMemory
TotalPhysicalMemory
1072742400

The same limit on images made from:

  • microsoft/windowsservercore
  • microsoft/nanoserver

I think it's coming from the Hyper-V layer in --isolation=hyperv mode, where a container is some sort of lightweight VM.

You can check isolation mode used for your existing container by docker inspect command.

Solution 4

With WSL2, as documented here, you can create a .wlsconfig file in your user home directory, type from the PowerShell:

notepad "$env:USERPROFILE/.wslconfig"

And the contents of the file to limit memory are the following:

[wsl2]
memory=3GB   # Limits VM memory in WSL 2 up to 3GB

In order to see if you are using WSL2 you can do so from the docker interface: enter image description here

Solution 5

NOTE: Switching to Linux containers and playing with the "Settings Resources > Advanced" options only modifies the VM resources for running Linux containers, and not Windows containers.

Solution

To adjust the amount of memory and CPU cores used for Windows containers you will need to use the --memory and --cpus argument flags when you run the image. For example:

docker run --name myWinImage --memory 4096m --cpus 2 -it -p ‘4096:7880’ --entrypoint powershell

Do NOT forget to append an "m" with number you set for the --memory flag as in "4096m" or it will have no effect. Also the memory flag has a short version -m 4096m.

Testing

You can verify that setting the flags worked by opening a Powershell terminal to the running container:

To check memory, run:

systeminfo | select-string 'Total Physical Memory'`

To check CPUs, run:

Get-WmiObject -class Win32_processor | Format-Table Name,NumberOfCores,NumberOfLogicalProcessors`

This article really helped me figure it out: https://www.sqlservercentral.com/blogs/default-resource-limits-for-windows-vs-linux-containers-in-docker-desktop

Share:
65,801
Andrey M.
Author by

Andrey M.

Updated on July 09, 2022

Comments

  • Andrey M.
    Andrey M. almost 2 years

    Does docker windows containers, with Docker Desktop for Windows, have default memory limit? I have an application that was crashing when I run it in the container, but when I tried to specify --memory 2048mb parameter to the docker run command it seems to run fine. At least in the scenario where it was crashing before. This gives me impression that there is default memory limit, but I could not find it in the documentation. So my question is there memory limit and if it is where it is documented?

  • Andrey M.
    Andrey M. about 7 years
    Thanks for your answer. No I don't have much containers. docker ps -a shows only 3 containers which I use.
  • Andrey M.
    Andrey M. about 7 years
    Thanks! Do you know where the limit come from? Is is docker setting or image setting?
  • Ivan
    Ivan about 7 years
    TBH no idea... could be from Hyper-V layer too, as Windows containers are based on it. Anyway you can set it with "-m" option, which solves it, does it not?
  • Ivan
    Ivan almost 7 years
    I see the same on Windows 2016 too
  • FizxMike
    FizxMike about 6 years
    I've found that -m has no effect in Windows Server 1709 (core) running docker EE preview. Did you ever find a way to "increase the memory for [windows server] containers?
  • Dan
    Dan about 5 years
    But how can you set this memory limit when using docker-compose?
  • Nicky Muller
    Nicky Muller almost 5 years
    These settings are for when Docker runs in Linux mode, which uses a full hyper-v vm. If you switch to Windows mode, these settings disappear.
  • Ryan Shillington
    Ryan Shillington almost 5 years
    @NickMuller In Windows mode, I'm guessing I can't run Linux VMs? Alpine & Ubuntu is what I want since that's how we deploy in prod.
  • Nicky Muller
    Nicky Muller almost 5 years
    True, but the question was about Windows containers, not Linux containers. By the way, there's a new feature where you can run Linux containers in Windows mode. Still experimental though. See: docs.microsoft.com/en-us/virtualization/windowscontainers/…
  • KMC
    KMC over 4 years
    @NickMuller : Adjusting Mem size for linux container is quite straight forward because it's part of Docker's settings. For Windows container, you need to go to Hyper-V Manager, and in the right pane, there's a Setting option under DockerDesktopVM. In Setting dialogbox, you can adjust mem size for container dynamic or static,
  • Oliver Hader
    Oliver Hader over 4 years
    In case memory limits kick in when switching from Windows to Linux containers, please see stackoverflow.com/questions/52904104/…
  • Murray Patterson
    Murray Patterson about 4 years
    I have a similar issue, however docker caps out at 1.943GiB (according to docker stats), even if I specify more memory (with -m)
  • Divisadero
    Divisadero about 4 years
    @Dan Did you find out?
  • Dan
    Dan about 4 years
    @Divisadero yeah add this mem_limit: 4096m
  • Victor
    Victor about 4 years
  • Max
    Max almost 4 years
    On windows 10, I found that once I adjusted the memory slider I was able to create containers that used more than 2 GB of memory (which was the default limit on my system).
  • Adhip Rebello
    Adhip Rebello almost 4 years
    @FizxMike Me too, I am running a Kafka container and although I have assigned 4GB memory using -m, the container stops when hitting 1GB...
  • Egor Hans
    Egor Hans over 2 years
    Side note: The settings also disappear in WSL2 Mode. It does tell you how to configure them instead though.
  • Kmeixner
    Kmeixner about 2 years
    Thanks. This worked for me. I had to restart my computer for it to take effect.