moby: run -d behaviour with Python apps - "print" / stdout not working
First posted at StackOverflow but now I think this could be a bug 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)
I thought it could be a problem with stdout when running detached, but even printing to stderr does not work:
from __future__ import print_function
print("App started", file=sys.stderr)
does not work either.
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
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Reactions: 23
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Fix python stdout not logged in Docker See https://github.com/moby/moby/issues/12447 Signed-off-by: janeczku <jan@rancher.com> — committed to janeczku/datadog-rancher-init by janeczku 9 years ago
- no logs in docker try https://github.com/moby/moby/issues/12447 — committed to PascalNoisette/rss-directory by PascalNoisette 8 years ago
- python3Console: Fix Dockerfile print on output On python the output is not printed on the Docker output while the code is being executed if the unbuffered option is not added when executing the file:... — committed to andreriesco/vscode-torizon-templates by andreriesco 2 years ago
Hi @jpdus.
docker attachsupposed to subscribe to contents of stdout/stderr streams after you attach (in real time). It’s not supposed to show you earlier output, by design (for that you can usedocker logs).If you modify your code to:
you will see:
so it’s actually working as expected.
Another point is you want to make sure you start python with unbuffered output option:
otherwise it’ll take longer for any output to be flushed to stdout.
Another proof of
docker attachactually works as expected is,docker run -i -tis basicallydocker create+run+attach. 😄does in fact solve the problem, many thanks @ahmetalpbalkan !
Stumbled upon this one as I had the same issue but after some thought looks to me the right way to have timely output from a python app inside a daemonized docker container would be to use python’s logging module.
From a simple test I used to shed some light:
docker logswill eventually give you that output, just after enough of it will have piled uppython -ugives instant output as said aboveimport logging+logging.warning("text")gives the expected result even without-uNote: I used python3.5 but that should not make any difference.
interesting. can you please try with this dockerfile. I cannot reproduce it:
if this doesn’t work either, can you please try adding
-tparameter todocker run?