Python exceptions in Docker logs marked as stream:

2019-03-04 07:55发布

I want parse and handle all errors from docker container, but python exceptions marked as stdout, when I expect stderr.

As example simple app.py

raise Exception("!")

Then I run this file in docker container. But in /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"}

标签: python docker
3条回答
Lonely孤独者°
2楼-- · 2019-03-04 08:08

Apart from the previous answer (and my comment), there is the attach of docker run from the doc http://docs.docker.com/reference/commandline/cli/#run -a, --attach=[] Attach to STDIN, STDOUT or STDERR

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-04 08:18

I had a misconception. I thought that the command of docker CLI does not affect the main logs (/var/lib/docker/containers/.../...-json.log)

But in case with:

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

json.log content:

{"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"}

But if I run container in background, stream become 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"}

I think this behavior implicitly.

查看更多
闹够了就滚
4楼-- · 2019-03-04 08:30

docker logs separating stdout from 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 
$
查看更多
登录 后发表回答