Python app does not print anything when running detached in docker

I have a Python (2.7) app which is started in my dockerfile:

    CMD ["python","main.py"]

main.py prints some strings when it is started and goes into a loop afterwards:

    print "App started"
    while True:
        time.sleep(1)

As long as I start the container with the -it flag, everything works as expected:

    $ docker run --name=myapp -it myappimage
    > App started

And I can see the same output via logs later:

    $ docker logs myapp
    > App started

If I try to run the same container with the -d flag, the container seems to start normally, but I can't see any output:

    $ docker run --name=myapp -d myappimage
    > b82db1120fee5f92c80000f30f6bdc84e068bafa32738ab7adb47e641b19b4d1
    $ docker logs myapp
    $ (empty)

But the container still seems to run;

    $ docker ps
    Container Status ...
    myapp     up 4 minutes ...

Attach does not display anything either:

    $ docker attach --sig-proxy=false myapp
    (working, no output)

Any ideas whats going wrong? Does "print" behave differently when ran in background?

Docker version:

    Client version: 1.5.0
    Client API version: 1.17
    Go version (client): go1.4.2
    Git commit (client): a8a31ef
    OS/Arch (client): linux/arm
    Server version: 1.5.0
    Server API version: 1.17
    Go version (server): go1.4.2
    Git commit (server): a8a31ef

Finally I found a solution to see Python output when running daemonized in Docker, thanks to @ahmetalpbalkan over at GitHub. Answering it here myself for further reference :

Using unbuffered output with

    CMD ["python","-u","main.py"]

instead of

    CMD ["python","main.py"]

solves the problem; you can see the output (both, stderr and stdout) via

    docker logs myapp

now!

From: stackoverflow.com/q/29663459

Back to homepage or read more recommendations: