Python exceptions in Docker logs marked as stream: stdout

前端 未结 3 1528
猫巷女王i
猫巷女王i 2021-01-26 18:17

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

相关标签:
3条回答
  • 2021-01-26 18:24

    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.

    0 讨论(0)
  • 2021-01-26 18:40

    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

    0 讨论(0)
  • 2021-01-26 18:42

    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 
    $
    
    0 讨论(0)
提交回复
热议问题