Docker日志中的Python异常标记为stream:stdout

时间:2015-06-18 09:00:33

标签: python docker

我希望解析并处理来自docker容器的所有错误,但是当我期望stderr时,python异常标记为stdout。

例如简单的 app.py

raise Exception("!")

然后我在docker容器中运行此文件。 但是在 / var / lib / docker / containers /.../...- json.log

{"log":"Traceback (most recent call last):\n","stream":"stdout","time":"2015-06-17T23:10:01.58636849Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stdout","time":"2015-06-17T23:10:01.586581081Z"}
{"log":"    raise Exception(\"!\")\n","stream":"stdout","time":"2015-06-17T23:10:01.586842665Z"}
{"log":"Exception: !\n","stream":"stdout","time":"2015-06-17T23:10:01.587373678Z"}

3 个答案:

答案 0 :(得分:3)

docker记录将stdout与stderr分开:

$ docker run -d --name foo busybox ls abcd
9a432862fb838b422d6b06446bc817d71cef09254059ec1ca92d0742580b81a4
$ docker logs foo > stdout.log 2>stderr.log
$ cat stdout.log 
$ cat stderr.log 
ls: abcd: No such file or directory
$

VS

$ docker run -d --name foo busybox ls /
5aff475fe0aa864c22633e7b915f7271e0a009b003371e9cdf2fbf1bae224709
$ docker logs foo > stdout.log 2>stderr.log
$ cat stdout.log 
bin
dev
etc
home
lib
lib64
linuxrc
media
mnt
opt
proc
root
run
sbin
sys
tmp
usr
var
$ cat stderr.log 
$

答案 1 :(得分:2)

除了上一个答案(以及我的评论)之外,还有来自文档http://docs.docker.com/reference/commandline/cli/#run docker run的{​​{1}}附加

答案 2 :(得分:1)

我有一种误解。我认为docker CLI的命令不会影响主日志(/ var / lib / docker / containers /.../...- json.log)

但是如果:

docker run -it my_python python /var/app.py

json.log内容:

{"log":"Traceback (most recent call last):\n","stream":"stdout","time":"2015-06-18T10:02:55.842010241Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stdout","time":"2015-06-18T10:02:55.842252975Z"}
{"log":"    raise Exception(\"error\")\n","stream":"stdout","time":"2015-06-18T10:02:55.842423153Z"}
{"log":"Exception: error\n","stream":"stdout","time":"2015-06-18T10:02:55.842754372Z"}

但如果我在后台运行容器,则流成为stderr:

docker run -d my_python python /var/app.py

{"log":"Traceback (most recent call last):\n","stream":"stderr","time":"2015-06-18T10:02:18.905673576Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stderr","time":"2015-06-18T10:02:18.90575399Z"}
{"log":"    raise Exception(\"error\")\n","stream":"stderr","time":"2015-06-18T10:02:18.905802834Z"}
{"log":"Exception: error\n","stream":"stderr","time":"2015-06-18T10:02:18.90616668Z"}

我认为这种行为是隐含的。