Docker Community Forums

Share and learn in the Docker community.

Docker-compose problem in python, only shows python output when python code exists

docker

(Lucaszanella) #1

I’m having a simple but annoying problem. When I launch my container through terminal using docker run, the python script it is supposed to run, runs normally. But when the same container is started through docker-compose, its output will only appear when the python code exits. So, for example:

print("hi")
from time import sleep
sleep(10)

It’ll display “hi” after 10 seconds when the container that runs it is called through docker-compose, and will display “hi” and exit after 10 seconds when called through docker run.

Docker version 17.09.1-ce, build 19e2cf6
docker-compose version 1.18.0, build 8dd22a9

Anyone knows what might be happening?


(David Maze) #2

I feel like it should be better documented than here, but by default Python will send each line to stdout (or other files) when a newline is written if the output file is a tty, and otherwise will buffer some amount in memory until the process exits. You can set the PYTHONUNBUFFERED environment variable, or tell docker-compose to allocate a tty.