docker-library / docker-library/python

Example Dockerfile should document to run python unbuffered

未关闭
#604 3 条评论 25 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Dockerfile
星标
2.8k
派生
1.1k
平均合并
3 天 1 小时
30 天内合并 PR
1

描述

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 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: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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 content.md 中“Create a Dockerfile in your python App Project”下的示例开始,然后查看 docs.docker.com/language/python/build-images/ 上的 Docker Python Guide。更新这两处文档,以说明用于容器日志的无缓冲 Python 输出,包含所选示例,并确保指导内容与所引用的 Python 行为一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
dockerfile, python
领域
documentation
Issue 类型
文档
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
50/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。