six*_*bit 5 linux logging stderr docker aws-codebuild
在构建 docker 镜像时,由RUN
命令引起的错误会打印到 stdout 而不是 stderr。例如:
如果我flooby
在我的 shell 上运行命令,由于找不到命令而失败的事实会打印到 stderr:
$ flooby 2> err.log
$ cat err.log
bash: flooby: command not found
Run Code Online (Sandbox Code Playgroud)
但是,如果我正在构建一个包含该行的 Docker 映像RUN flooby
,尽管(可能)在容器/映像/正在构建的任何内容中,错误将打印到 stderr,docker 将其打印到 stdout:
$ docker build -t stderr-test . 2> err.log
Sending build context to Docker daemon 121.1MB
Step 1/2 : FROM ruby:2.6
---> d98e4013532b
Step 2/2 : RUN flooby
---> Running in 118f7d467b43
/bin/sh: 1: flooby: not found
$ cat err.log
The command '/bin/sh -c flooby' returned a non-zero code: 127
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到,本应打印到 stderr 的内容被打印到了 stdout,而打印到 stderr 的是Docker 的消息,即运行出现问题flooby
并带有错误代码。
当我想聚合日志并能够根据日志级别(信息/错误)过滤它们时,这会导致 CI 出现问题。知道flooby
返回非零代码 127 毫无价值。我需要知道运行 flooby 的错误输出是什么。
有谁知道如何docker build
从构建指令打印错误消息以打印到 stderr 而不是 stdout?