Docker 主机与容器中的 Ulimits [英] Ulimits in Docker host vs container

查看:95
本文介绍了Docker 主机与容器中的 Ulimits的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到这个问题的直接答案,但这里是:

I wasn't able to find straight answer, to this question, but here it is:

假设我有一个最大打开文件数为 1024 的主机:

Let's say that I have a host which has max open files 1024:

[root@host]# ulimit -a
open files                      (-n) 1024

以及在该主机中运行的 docker 容器:

and a docker container running in that host with:

[root@container]# ulimit -a
open files                      (-n) 1048576

如果我尝试打开超过 1024 个文件,我会在容器中遇到任何问题吗?我认为在这种情况下容器的真正限制是 1024 个文件.你怎么看?

So will I have any problem in container if I try to open more than 1024 files? I think the real limit in this case for container will be 1024 files. What do you think?

推荐答案

虽然有点晚了,但还是想解开ulimit区别的疑惑.

Although its a little bit late, I just want to clear the doubts about the difference in ulimit.

如果你在运行容器的时候做了net set,那么容器内显示的ulimit值来自宿主机操作系统.那么问题来了,为什么在从主机运行相同的命令时会看到不同的值?

If you do net set the value when running the container, the ulimit value displayed within the container comes from the host OS. The question is then why are you seeing a different value when running the same command from the host?

这是因为在主机中运行命令时,它显示了它的软限制.另一方面,容器显示的值是主机操作系统的硬限制.这样做的原因是您可以越过软限制.所以从某种意义上说,硬限制其实才是真正的限制.您可以在此 链接中找到有关 ulimit 的更多信息.

This is because when running the command in the host, it is showing its soft limit. On the other hand, the value that the container is showing is the hard limit of the host OS. The reason for this is you are allowed to cross the soft limit. So in a sense, hard limit is actually the real limit. You can find more about ulimit in this link.

要查看硬限制,只需键入以下命令

To see the hard limit just type the following command

ulimit -Hn

您将看到这些值匹配.

注意你不能越过硬限制,但如果你是根,你可以增加它.

N.B. You can not cross the hard limit but you can increase it if you are the root.

可以在 Docker 配置中使用 LimitNOFILE 设置打开文件的限制,或者可以将它们传递给 docker run 命令:

Limits on open files can be set in the Docker configuration using LimitNOFILE, or they can be passed to the docker run command:

$ docker run -it --ulimit nofile=1024:2048 ubuntu bash -c 'ulimit -Hn && ulimit -Sn'
2048
1024

请参阅此处了解设置详情Docker 中的 ulimit.

See here for elaboration on setting ulimits in Docker.

请注意,此限制可以设置为高于操作系统的硬限制,这可能会导致问题.

Note that this limit can be set higher than the OS's hard limit, which can cause trouble.

这篇关于Docker 主机与容器中的 Ulimits的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆