问题描述
我有一个在我的 dockerfile 中启动的 Python (2.7) 应用程序:
I have a Python (2.7) app which is started in my dockerfile:
CMD ["python","main.py"]
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)
只要我用 -it 标志启动容器,一切都会按预期进行:
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
如果我尝试使用 -d 标志运行同一个容器,容器似乎正常启动,但我看不到任何输出:
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 也不显示任何内容:
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 版本:
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
推荐答案
我终于找到了在 Docker 中运行守护进程时查看 Python 输出的解决方案,感谢@ahmetalpbalkan 在 GitHub.自己在这里回答以供进一步参考:
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 :
使用无缓冲输出
CMD ["python","-u","main.py"]
代替
CMD ["python","main.py"]
解决问题;您可以通过
docker logs myapp
现在!
这篇关于在 docker 中分离运行时,Python 应用程序不打印任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!