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
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)
I had to use PYTHONUNBUFFERED=1
in my docker-compose.yml file to see the output from django runserver.
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.
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/
Try to add these two environment variables to your solution PYTHONUNBUFFERED=1
and PYTHONIOENCODING=UTF-8
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" ]