简体   繁体   English

Docker windows 容器 memory 限制

[英]Docker windows container memory limit

Does docker windows containers, with Docker Desktop for Windows , have default memory limit? docker windows 容器和Docker 桌面 Windows是否有默认的 memory 限制? 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.我有一个应用程序在容器中运行时崩溃,但是当我尝试为docker run命令指定--memory 2048mb参数时,它似乎运行良好。 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.这给我的印象是有默认的 memory 限制,但我在文档中找不到它。 So my question is there memory limit and if it is where it is documented?所以我的问题是有 memory 限制,是否有记录?

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).根据关于 Docker for windows Github 问题的讨论( https://github.com/moby/moby/issues/31604 ),当 Docker for Windows 在 Windows 10 下运行时,它实际上是使用 Hyper-V 隔离模型(和进程模型在 Win 10 场景中无法访问)。

And in that isolation type, your container is run inside a lightweight VM, which DOES have a default limit, and it is 1 Gb.在这种隔离类型中,您的容器在轻量级 VM 中运行,该 VM 具有默认限制,并且为 1 Gb。 So if you want to have more memory you should use -m param.所以如果你想拥有更多的内存,你应该使用 -m 参数。

If using a Linux container如果使用 Linux 容器

For me, on Windows 10 using Docker Desktop, I could not get the --memory= and --cpus= options to work.对我来说,在使用 Docker Desktop 的 Windows 10 上,我无法让--memory=--cpus=选项工作。 Here's what does work:以下是有效的方法:

  1. Right click on the Docker whale in the system tray and choose "Settings"右键单击系统托盘中的 Docker 鲸鱼,然后选择“设置”
  2. Go to "Resources -> Advanced" on the left转到左侧的“资源->高级”
  3. Set how many CPUs and memory is available to containers here.在此处设置容器可用的 CPU 和内存数量。

在此处输入图像描述

Surprise!惊喜! Surprise!惊喜!

I have 32Gb RAM on my host but I can see only 1Gb RAM given to Windows containers:我的主机上有 32Gb RAM,但我只能看到分配给 Windows 容器的 1Gb RAM:

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微软/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.我认为它来自--isolation=hyperv模式下的 Hyper-V 层,其中容器是某种轻量级 VM。

You can check isolation mode used for your existing container by docker inspect command.您可以通过docker inspect命令检查用于现有容器的隔离模式。

With WSL2 , as documented here , you can create a .wlsconfig file in your user home directory, type from the PowerShell:使用 WSL2 ,如此所述,您可以在用户主目录中创建一个 .wlsconfig 文件,从 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:为了查看您是否使用 WSL2,您可以从 docker 界面执行此操作: 在此处输入图像描述

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.注意:切换到 Linux 容器并使用“设置资源 > 高级”选项只会修改运行 Linux 容器的 VM 资源,而不是 Windows 容器。

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.要调整用于 Windows 容器的内存和 CPU 内核数量,您需要在运行映像时使用--memory--cpus参数标志。 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.不要忘记在“4096m”中为--memory标志设置的数字附加一个“m”,否则它将无效。 Also the memory flag has a short version -m 4096m .内存标志也有一个短版本-m 4096m

Testing测试

You can verify that setting the flags worked by opening a Powershell terminal to the running container:您可以通过将Powershell终端打开到正在运行的容器来验证设置标志​​是否有效:

To check memory, run:要检查内存,请运行:

systeminfo | select-string 'Total Physical Memory'`

To check CPUs, run:要检查 CPU,请运行:

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这篇文章真的帮我弄清楚了: https ://www.sqlservercentral.com/blogs/default-resource-limits-for-windows-vs-linux-containers-in-docker-desktop

Run Docker QuickStart Terminal运行 Docker 快速入门终端

Remove the default vm:删除默认虚拟机:

docker-machine rm default

Re-create the default vm: Depending on your requirements you can change the values for different parameters below.重新创建默认虚拟机:根据您的要求,您可以更改以下不同参数的值。

docker-machine create -d virtualbox --virtualbox-cpu-count=2 --virtualbox-memory=4096 --virtualbox-disk-size=50000 default

Then do然后做

docker-machine stop
exit

Now open the Docker quickstart terminal again现在再次打开 Docker 快速启动终端

Now when you run and create a new container you will have more ram memory by default.现在,当您运行并创建一个新容器时,默认情况下您将拥有更多的 RAM 内存。 But be careful it will delete already all pulled docker images.但要小心,它会删除所有拉取的 docker 镜像。

We recently had a very similar problem and question and therefore made some experiments with docker memory on windows:我们最近遇到了一个非常相似的问题和疑问,因此在 windows 上用 docker memory 做了一些实验:

It seems that it heavily depends on you configuration.似乎它在很大程度上取决于您的配置。 If you run docker containers in, lets call it hyper-v mode, the memory limit seems to be about 512mb.如果您在 docker 容器中运行,我们称之为 hyper-v 模式,内存限制似乎约为 512mb。 You can extend the given memory with the "-m" option for docker run.您可以使用 docker run 的“-m”选项扩展给定的内存。 Assigning 2 gb have not been a problem.分配 2 GB 不是问题。

Unfortunately, its totally different for windows server containers.不幸的是,Windows 服务器容器完全不同。 There the starting memory limit is 1gb and you can decrease it with the "-m" option.那里的起始内存限制为 1gb,您可以使用“-m”选项减少它。 We did not find a way to increase the memory for those containers.我们没有找到增加这些容器内存的方法。

How you see your mode/isolation level:您如何看待您的模式/隔离级别:

docker info -f "{{ .Isolation }}"

hyperv - hyper-v mode; hyperv - hyper-v 模式; process is windows server进程是windows server

Some additional notes: Its very hard to find a command that outputs the available memory in a container.一些附加说明:很难找到输出容器中可用内存的命令。 We had created our own performance test for reliable results.我们创建了自己的性能测试以获得可靠的结果。

I think it might be that you have too much dead containers.我认为可能是您的死容器太多。 Maybe try docker rm $(docker ps -a -q) to remove all the exited container then retry running the container you want.也许尝试docker rm $(docker ps -a -q)删除所有退出的容器,然后重试运行您想要的容器。

You can also set the memory used by docker by editing the json file C:\Users\Personal\AppData\Roaming\Docker\settings.json .您还可以通过编辑 json 文件 C:\Users\Personal\AppData\Roaming\Docker\settings.json 来设置 docker 使用的内存。 Look for a property called MemoryMiB and update its value to be the number of megabytes you want your docker installation to use.查找名为 MemoryMiB 的属性并将其值更新为您希望 docker 安装使用的兆字节数。

To create a .wslconfig file, simply open your File Explorer, and type and enter %UserProfile% to go to your profile directory in Windows.要创建.wslconfig文件,只需打开您的文件资源管理器,然后键入并输入%UserProfile%到 go 到您在 Windows 中的配置文件目录。

Docker or WSL2 by default does not create these config files so we should do it ourselves. Docker 或 WSL2 默认情况下不会创建这些配置文件,所以我们应该自己做。 Create a new file called.wslconfig (make sure there are no .txt at the end)创建一个名为.wslconfig 的新文件(确保末尾没有.txt

Add the (either all or some of the) following commands in that file to configure docker在该文件中添加(全部或部分)以下命令以配置 docker

# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole 
numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=2
# Specify a custom Linux kernel to use with your installed distros.
kernel=C:\\temp\\myCustomKernel
# Sets additional kernel parameters, in this case enabling older Linux base     images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB
# Sets swapfile path location, default is     %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=C:\\temp\\wsl-swap.vhdx
# Disable page reporting so WSL retains all allocated memory claimed from     Windows and releases none back when free
pageReporting=false
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true
# Disables nested virtualization
nestedVirtualization=false
# Turns on output console showing contents of dmesg when opening a WSL 2     distro for debugging
debugConsole=true

Now you can set your desired Memory and virtual processor config.现在您可以设置所需的Memory虚拟处理器配置。 After that close your Docker desktop and open it again.之后关闭您的 Docker 桌面并再次打开它。

that's all, Enjoy就是这样,享受

No default limits.没有默认限制。 Documented here记录在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM