docker-library / docker-library/python

Example Dockerfile should document to run python unbuffered

Offen
#604 3 Kommentare 25 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Dockerfile
Sterne
2.8k
Forks
1.1k
Ø Merge
3 T. 1 Std.
Gemergte PRs (30 T.)
1

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem Beispiel in content.md unter „Create a Dockerfile in your python App Project“ und sehen Sie sich anschließend den Docker Python Guide unter docs.docker.com/language/python/build-images/ an. Aktualisieren Sie beide Dokumentationsstellen, um die ungepufferte Python-Ausgabe für Container-Logs zu erklären, das ausgewählte Beispiel aufzunehmen und sicherzustellen, dass die Anleitung dem genannten Python-Verhalten entspricht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
dockerfile, python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
50/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.