You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Example Dockerfile should document to run python unbuffered #604
When interactive, the stdout stream is line-buffered. Otherwise, it is block-buffered like regular text files. The stderr stream is line-buffered in both cases. You can make both streams unbuffered by passing the -u command-line option or setting the PYTHONUNBUFFERED environment variable. Changed in version 3.9: Non-interactive stderr is now line-buffered instead of fully buffered.
When running a container non-interactively, no output from the python script or daemon are flushed to stdout until the block buffer is full (generally DEFAULT_BUFFER_SIZE = 8192) or the container terminates. For daemons which do not terminate and which may not have much logging over its lifetime, nothing may be logged at all.
This makes viewing / tailing the container logs in realtime which by default captures stdout problematic, especially when viewing the logs in conjunction with other services.
Example docker-compose.yaml which demonstrates this issue.
---
services:
demo:
image: python:3command:
- python
- -c
- | import time for i in range(10): print(i) time.sleep(1)
No output is written until the container terminates.
Activity
daveisfera commentedon Nov 16, 2021
Just as a heads up, this isn't just a Python thing, but is standard practice for
stdoutandstderr. It's primarily done for performance reasons.nijave commentedon Dec 11, 2023
This isn't a problem when using StreamHandler in the
loggingpackage. It flushes after any call to a log method (info,warning, etc)Sbrjt commentedon Jun 27, 2025
I used
python3 -u -m uvicorn app.main:appin mydocker-compose.yml, but it didn't seem to work.Then I tried
and this worked. 🤷
vivodi commentedon Oct 11, 2026
The issue still persists under Python 3.14:
The issue is resolved by adding
PYTHONUNBUFFERED=1: