Skip to content Skip to sidebar Skip to footer

Python Exceptions In Docker Logs Marked As Stream: Stdout

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 th

Solution 1:

docker logs separating stdout from stderr:

$ docker run -d --name foo busybox ls abcd9a432862fb838b422d6b06446bc817d71cef09254059ec1ca92d0742580b81a4
$ docker logs foo > stdout.log2>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.log2>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 
$

Solution 2:

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

Solution 3:

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.

Post a Comment for "Python Exceptions In Docker Logs Marked As Stream: Stdout"