5

Consider this issue.py file:

import subprocess

print('Calling subprocess...')
subprocess.run(['python', '--version'])
print('Subprocess is done!')

Executing python issue.py manually yields what I expect:

Calling subprocess...
Python 3.9.0
Subprocess is done!

However, if I execute this inside a Docker container, something weird happens:

$ docker run --rm -v $(pwd):/issue python:3.9.0 python /issue/issue.py
Python 3.9.0
Calling subprocess...
Subprocess is done!

How can I fix this, to make Docker respect the correct output order?

Notes:

  • This problem also happens with stderr, although the above MCVE does not show it.
  • The MCVE uses the python image directly but in my real use case I have a custom image from a custom Dockerfile which uses FROM python.
  • Using capture_output=True in the subprocess.run call and then printing the captured output is not an option for me because my real use case invokes a subprocess that prints information over time to stdout (unlike python --version) and I cannot wait for it to complete to print the entire output only after that.
Pedro A
  • 3,989
  • 3
  • 32
  • 56
  • 1
    Does the `python -u` flag or `PYTHONUNBUFFERED=1` environment variable help? ([What is the use of PYTHONUNBUFFERED in docker file?](https://stackoverflow.com/questions/59812009/what-is-the-use-of-pythonunbuffered-in-docker-file); [`Docker logs` erroneously appears empty until container stops](https://stackoverflow.com/questions/58701233/docker-logs-erroneously-appears-empty-until-container-stops).) – David Maze Apr 02 '21 at 00:08
  • @DavidMaze Yes!! That worked!! Awesome, thank you very much. Can you add this as an answer so I can accept it? – Pedro A Apr 02 '21 at 05:09

1 Answers1

0

As @DavidMaze pointed out in a comment, I just needed to set the PYTHONUNBUFFERED environment variable to 1. This can be done for example with:

docker run --rm -e PYTHONUNBUFFERED=1 -v $(pwd):/issue python:3.9.0 python /issue/issue.py
Pedro A
  • 3,989
  • 3
  • 32
  • 56