Example Dockerfile should document to run python unbuffered
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 50/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- dockerfile, python
调研方向
从 content.md 中“Create a Dockerfile in your python App Project”下的示例开始,然后查看 docs.docker.com/language/python/build-images/ 上的 Docker Python Guide。更新这两处文档,以说明用于容器日志的无缓冲 Python 输出,包含所选示例,并确保指导内容与所引用的 Python 行为一致。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Dockerfile
- 星标
- 2.8k
- 派生
- 1.1k
- 平均合并
- 3 天 1 小时
- 30 天内合并 PR
- 1
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
docker-library/python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
docker-library/python#1080 · 8 条评论 ·
-
难度 4/5 3-5 天 新手友好度 45/100
docker-library/python#1121 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 45/100
docker-library/python#1082 · 10 条评论 · 36 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 55/100
docker-library/python#1076 · 6 条评论 ·
-
难度 3/5 1-2 天 新手友好度 52/100
docker-library/python#1071 · 7 条评论 ·