docker 无法看到 STDERR 输出

Not able to see STDERR Output with docker

我正在 运行 宁 php docker 图像 (php:5.6-apache) 它有 apache 的错误和访问日志分别重定向到 STDERR 和 STDOUT使用符号链接。

当我 运行 前台的 docker 图像或访问 docker 容器日志时,我可以看到 STDOUT 输出。但是我没有看到任何错误(即使我生成 php 错误)。

知道为什么会这样吗?我该如何解决?

我 运行宁 docker Mac(但我认为这没有任何区别)

谢谢

access.log -> /dev/stdout
error.log -> /dev/stderr
other_vhosts_access.log -> /dev/stdout

编辑/解决: 正如@BMitch 在下面提到并证明的那样,STDERR 重定向工作正常。 问题出在 PHP 配置上。如果我用 error_log() 记录错误,它会输出到日志中。但是如果我有一个 php 错误,比如调用一个未定义的函数,这个错误就永远不会出现在日志中。这似乎有点不一致。无论如何,...

我必须在 /usr/local/etc/php/ 中创建一个 php.ini 文件并添加这两个参数:

log_errors = On
error_log = /var/log/apache2/error.log

然后重新启动 docker 容器。这导致所有 PHP 错误被记录并输出到 STDERR。有关示例,请参见@German 的回答。

如果您想在 apache2 日志中看到 php 错误,您必须激活它们。为此,您必须将 php.ini 文件写入文件夹 /usr/local/etc/php.

我在存储库中写了一个示例,以便可以看到该示例。

https://github.com/nitzap/Whosebug-docker-php-error-log

如果您想查看 stdout 和 stderr 日志,可以在您的容器外使用以下命令:

docker logs <name_container>

现在,如果您想在日志中保持附加状态,您可以像在 tail 命令中那样添加 -f 选项。

docker logs <name_container> -f

我无法重现您的情况。如果以下方法没有帮助,请提供 mcve 您的错误。

基本 Dockerfile:

$ cat Dockerfile 
FROM php:5.6-apache
COPY . /var/www/html/

唯一php是这个文件产生错误:

$ cat error.php 
<?
error_log("Hello error log.")
?>

构建并运行它:

$ docker build -t test-php .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM php:5.6-apache
 ---> f16436448ebd
Step 2/2 : COPY . /var/www/html/
 ---> Using cache
 ---> cfe66485e2cc
Successfully built cfe66485e2cc
Successfully tagged test-php:latest

$ docker run -p 8080:80 -d --name test-php test-php
7f9a1836a8157963966b583579dff94c6413292547b84d22957add77ad2d8e14

Curl 如预期的那样为空,但调用它会在日志中生成错误:

$ curl localhost:8080/error.php

显示标准输出日志,将错误重定向到 /dev/null:

$ docker logs test-php 2>/dev/null
172.17.0.1 - - [31/May/2017:00:06:37 +0000] "GET /error.php HTTP/1.1" 200 174 "-" "curl/7.38.0"

显示 stderr 日志,将 stdout 重定向到 /dev/null

$ docker logs test-php >/dev/null
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed May 31 00:06:25.064546 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.30 configured -- resuming normal operations
[Wed May 31 00:06:25.064584 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
[Wed May 31 00:06:37.833470 2017] [:error] [pid 17] [client 172.17.0.1:50040] Hello error log.

注意错误输出的最后一行。

在我的例子中,我错过了这个神奇的设置(默认为"no"):

#/etc/php/7.0/fpm/pool.d/www.conf
catch_workers_output = yes

在这里找到的