Example Dockerfile should document to run python unbuffered
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 50/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- dockerfile, python
- Domain
- documentation
Research direction
Start with the example in content.md under “Create a Dockerfile in your python App Project,” then review the Docker Python Guide at docs.docker.com/language/python/build-images/. Update both documentation locations to explain unbuffered Python output for container logs, include the chosen example, and ensure the guidance matches the cited Python behavior.
Written by the indexing model from the issue text.
Description
The example Dockerfile in the Create a Dockerfile in your python App Project section in the image documentation should have
ENV PYTHONUNBUFFERED=1
or add the -u option to
CMD [ "python", "-u", "./your-daemon-or-script.py" ]
From https://docs.python.org/3/library/sys.html#sys.stdout:
When interactive, the
stdoutstream is line-buffered. Otherwise, it is block-buffered like regular text files. Thestderrstream is line-buffered in both cases. You can make both streams unbuffered by passing the-ucommand-line option or setting thePYTHONUNBUFFEREDenvironment 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:3
command:
- python
- -c
- |
import time
for i in range(10):
print(i)
time.sleep(1)
No output is written until the container terminates.
It took me a while to figure out what the problem was, believing it was a problem with Docker not flushing logs to the driver (with --log-opt mode=non-blocking) or getting blocked (with --log-opt mode=blocking - the default), until I realised it was a problem specific to Python. Adding changes to the Python image documentation would help users with similar problems. E.g. https://github.com/moby/moby/issues/12447#issuecomment-94416623 and https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker
The Docker Python Guide docs, https://docs.docker.com/language/python/build-images/ should also be updated.
- Dominant language
- Dockerfile
- Stars
- 2.8k
- Forks
- 1.1k
- Avg merge
- 3d 1h
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from docker-library/python
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
docker-library/python#1080 · 8 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
docker-library/python#1121 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
docker-library/python#1082 · 10 comments · 36 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
docker-library/python#1076 · 6 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
docker-library/python#1071 · 7 comments ·
All issues in docker-library/python
Similar issues
-
Link Checker Report Openautomated issue report
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
felladrin/uo-is-not-dead#176 · 1 comment ·
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Qiskit/qiskit-addon-sqd#376 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Seeed-Studio/wiki-documents#5655 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
MystenLabs/sui#28056 · 1 comment ·