PHP Docker - 是否可以在运行时设置 ulimit?

PHP Docker - Is it possible to set ulimit at runtime?

在我正在处理的项目中,xdebug 需要大约 1 小时才能生成覆盖率。我已经尝试过 phpdbg,但在我的测试套件中遇到以下错误:

stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
It is set to 1024, but you have descriptors numbered at least as high as 7228.
 --enable-fd-setsize=8192 is recommended, but you may want to set it
to equal the maximum number of open files supported by your system,
in order to avoid seeing this error again at a later date.

我正在使用 php:7.0.33-fpm(是的,我知道,我们正在努力升级 PHP 版本 :))

在命令行中使用ulimit或依赖docker-compose to set it,输出仍然认为它设置为1024。实际上,我容器中的ulimit -n当前输出实际上已经结束如果我不尝试通过其他机制设置它,则为 100 万。

是否有任何机制可以通过运行时设置,而无需从头开始编译我们自己的 php 容器?

可以使用以下 PHP 片段触发此警告:

<?php

$fds = [];
for ($i = 0; $i < PHP_FD_SETSIZE; $i++) { // PHP_FD_SETSIZE how FD_SETSIZE is exposed to userland
    $fds[] = fopen(__FILE__, 'r');
}

$read = $fds;
$write = [];
$except = [];

echo sprintf("FD_SETSIZE=%d", PHP_FD_SETSIZE) . "\n";

stream_select($read, $write, $except, 0);
/*
 * Warning: stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
 * It is set to 1024, but you have descriptors numbered at least as high as 1027.
 *   --enable-fd-setsize=2048 is recommended, but you may want to set it
 * to equal the maximum number of open files supported by your system,
 * in order to avoid seeing this error again at a later date. in /in/ZfBsg on line 14
 */

FD_SETSIZE 是由 POSIX 定义的常量(参见 select(2) manpage)。虽然警告使您相信可以减轻此警告 通过使用 --enable-fd-setsize=[some-larger-value] 重新编译 php,这是不正确的。 FD_SETSIZE 无法更改 运行时,因为它的值是在编译内核时确定的。

我查看了 PHP 源代码,我不确定为什么 --enable-fd-setsize 存在,因为它不存在 似乎可以做任何事情(虽然我没有在 Windows 上测试过)。

这个问题的唯一解决方案似乎是:

  • 避免stream_select
  • 避免打开很多文件