【问题标题】:Not able to see STDERR Output with docker无法使用 docker 查看 STDERR 输出
【发布时间】:2017-10-30 10:43:15
【问题描述】:

我正在运行一个 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

编辑/解决: 正如@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 的答案。

【问题讨论】:

  • 您安装了任何卷吗?
  • @BMitch,是的,但只有 /var/www

标签: php apache docker stderr


【解决方案1】:

我无法重现您的情况。如果以下内容没有帮助,请提供您的错误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"

显示标准错误日志,将标准输出重定向到 /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.

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

【讨论】:

  • 是的,如果您直接拨打error_log(),它似乎可以工作。但是如果你调用error_pug()(当然不存在),日志不会显示任何错误。你只会在浏览器中得到一个错误。
  • 今天早上我正在和这个作斗争,但没时间了。我相信 php.ini 需要更新,但我无法应用我的更改(可能为 5.6 构建编写了错误的文件)。这个问题在 php 方面更详细:stackoverflow.com/questions/1053424/…
  • 这对我来说没有按预期工作,结果所有内容都记录为stderr。检查原始日志数据(假设默认 json-file 日志驱动程序)less &lt;$(docker inspect -f '{{.LogPath}}' CONTAINER)
  • 要禁用输出,请将其重定向到&amp;-。这是关闭描述符的 bash 方式。
【解决方案2】:

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

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

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

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

docker logs <name_container>

现在如果你想在日志中保持连接,你可以像在 tail 命令中一样添加 -f 选项。

docker logs <name_container> -f

【讨论】:

    【解决方案3】:

    就我而言,我错过了这个神奇的设置(默认为“否”):

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

    在这里找到它Symfony logs to stdout inside Docker container

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2021-12-25
      • 2016-09-08
      • 2018-01-05
      • 1970-01-01
      • 2011-05-03
      相关资源
      最近更新 更多