Python app does not print anything when running detached in docker

前端 未结 11 1422
不思量自难忘°
不思量自难忘° 2020-11-29 16:54

I have a Python (2.7) app which is started in my dockerfile:

CMD [\"python\",\"main.py\"]

main.py prints some strings when it is s

相关标签:
11条回答
  • 2020-11-29 17:10

    Since I haven't seen this answer yet:

    You can also flush stdout after you print to it:

    import time
    
    if __name__ == '__main__':
        while True:
            print('cleaner is up', flush=True)
            time.sleep(5)
    
    0 讨论(0)
  • 2020-11-29 17:10

    I had to use PYTHONUNBUFFERED=1 in my docker-compose.yml file to see the output from django runserver.

    0 讨论(0)
  • 2020-11-29 17:11

    See this article which explain detail reason for the behavior:

    There are typically three modes for buffering:

    • If a file descriptor is unbuffered then no buffering occurs whatsoever, and function calls that read or write data occur immediately (and will block).
    • If a file descriptor is fully-buffered then a fixed-size buffer is used, and read or write calls simply read or write from the buffer. The buffer isn’t flushed until it fills up.
    • If a file descriptor is line-buffered then the buffering waits until it sees a newline character. So data will buffer and buffer until a \n is seen, and then all of the data that buffered is flushed at that point in time. In reality there’s typically a maximum size on the buffer (just as in the fully-buffered case), so the rule is actually more like “buffer until a newline character is seen or 4096 bytes of data are encountered, whichever occurs first”.

    And GNU libc (glibc) uses the following rules for buffering:

    Stream               Type          Behavior
    stdin                input         line-buffered
    stdout (TTY)         output        line-buffered
    stdout (not a TTY)   output        fully-buffered
    stderr               output        unbuffered
    

    So, if use -t, from docker document, it will allocate a pseudo-tty, then stdout becomes line-buffered, thus docker run --name=myapp -it myappimage could see the one-line output.

    And, if just use -d, no tty was allocated, then, stdout is fully-buffered, one line App started surely not able to flush the buffer.

    Then, use -dt to make stdout line buffered or add -u in python to flush the buffer is the way to fix it.

    0 讨论(0)
  • 2020-11-29 17:12

    If you want to add your print output to your Flask output when running docker-compose up, add the following to your docker compose file.

    web:
      environment:
        - PYTHONUNBUFFERED=1
    

    https://docs.docker.com/compose/environment-variables/

    0 讨论(0)
  • 2020-11-29 17:15

    Try to add these two environment variables to your solution PYTHONUNBUFFERED=1 and PYTHONIOENCODING=UTF-8

    0 讨论(0)
  • 2020-11-29 17:20

    If you aren't using docker-compose and just normal docker instead, you can add this to your Dockerfile that is hosting a flask app

    ARG FLASK_ENV="production"
    ENV FLASK_ENV="${FLASK_ENV}" \
        PYTHONUNBUFFERED="true"
    
    CMD [ "flask", "run" ]
    
    0 讨论(0)
提交回复
热议问题