无法通过docker查看STDERR输出

swb*_*dit 2 php apache stderr docker

我正在运行一个php docker映像(php:5.6-apache),该映像具有apache的错误,并使用符号链接将访问日志分别重定向到STDERR和STDOUT。

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

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

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

谢谢

access.log -> /dev/stdout
error.log -> /dev/stderr
other_vhosts_access.log -> /dev/stdout
Run Code Online (Sandbox Code Playgroud)

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

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

log_errors = On
error_log = /var/log/apache2/error.log
Run Code Online (Sandbox Code Playgroud)

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

BMi*_*tch 6

我无法重现您的情况。如果下面没有帮助,请提供MCVE你的错误。

基本的Dockerfile:

$ cat Dockerfile 
FROM php:5.6-apache
COPY . /var/www/html/
Run Code Online (Sandbox Code Playgroud)

唯一的php是此文件,会产生错误:

$ cat error.php 
<?
error_log("Hello error log.")
?>
Run Code Online (Sandbox Code Playgroud)

生成并运行它:

$ 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
Run Code Online (Sandbox Code Playgroud)

Curl为空,但是调用它会在日志中生成错误:

$ curl localhost:8080/error.php
Run Code Online (Sandbox Code Playgroud)

显示标准输出日志,将错误重定向到/ 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"
Run Code Online (Sandbox Code Playgroud)

显示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.
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,如果你直接调用 `error_log()`,它似乎可以工作。但是如果你调用 `error_pug()`(当然不存在),日志不会显示任何错误。您只会在浏览器中收到错误消息。 (2认同)