trbabb
trbabb

Reputation: 2095

`Docker logs` erroneously appears empty until container stops

When running a Docker container that calls a Python process, running docker logs ##### will return nothing, despite events happening inside the container which emit to stdout. Noting appears in the logs until I run docker stop ######, in which case the expected output is returned. The same is true with docker logs -f #####— nothing appears, even when expected, until after the container is stopped.

This is very cumbersome for debugging. Why might this happen? Is there a setting I can change to ensure the logs are updated in real-time?

Upvotes: 5

Views: 2167

Answers (1)

Leandro Silva
Leandro Silva

Reputation: 96

This might be happening because stdout and stderr are buffered streams.

When interactive, stdout and stderr streams are line-buffered. Otherwise, they are block-buffered like regular text files. You can override this value with the -u command-line option.

Try adding the -u flag.

CMD [ "python", "-u", "./your_script.py" ]

Or, as pointed out by David Maze, you could set the PYTHONUNBUFFERED environment variable and achieve the same result.

ENV PYTHONUNBUFFERED=1

You could also flush stdout everytime you call print().

sys.stdout.flush()

Alternatively, see the logging module.

Upvotes: 8

Related Questions