在Docker运行时,Python应用程序不会打印任何内容 [英] Python app does not print anything when running detached in docker
问题描述
CMD [python,main .py]
main.py 在启动时打印一些字符串然后进入一个循环:
打印应用程序启动
,而True:
time.sleep (1)
只要我使用-it标志启动容器,一切都按预期工作:
$ docker run --name = myapp -it myappimage
>应用程序开始
稍后我可以通过日志看到相同的输出:
$ docker记录myapp
>应用程序启动
如果我尝试使用-d标志运行相同的容器,容器似乎开始通常情况下,我看不到任何输出:
$ docker run --name = myapp -d myappimage
> b82db1120fee5f92c80000f30f6bdc84e068bafa32738ab7adb47e641b19b4d1
$ docker日志myapp
$(空)
但容器仍然似乎运行;
$ docker ps
容器状态...
myapp up 4分钟..
附件不显示任何内容:
$ docker attach --sig-proxy = false myapp
(工作,无输出)
任何想法出什么问题?在背景中运行时,print的行为是否有所不同?
Docker版本:
客户端版本:1.5.0
客户端API版本:1.17
Go版本(客户端):go1.4.2
Git提交(客户端):a8a31ef
操作系统/客户端):linux / arm
服务器版本:1.5.0
服务器API版本:1.17
转到版本(服务器):go1.4.2
Git提交(服务器):a8a31ef
最后,我发现一个解决方案,在Docker,感谢@ahmetalpbalkan在 GitHub 。在这里回答,以供进一步参考:
使用无缓冲输出与
CMD [python, - u,main.py]
/ p>
CMD [python,main.py]
解决问题;您可以通过
看到输出(stderr和stdout),docker记录myapp
现在!
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!
这篇关于在Docker运行时,Python应用程序不会打印任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!