执行代码后停止Docker容器
为了深入了解Docker,我创build了一个执行python脚本的dockerfile。 它工作正常,但脚本执行后,容器崩溃。 我怎样才能修改我的dockerfile,以便在执行后销毁容器,而不是让容器崩溃,并始终重新启动?
Dockerfile:
FROM python:3 ADD aa.py / CMD [ "python", "./aa.py" ]
python:
print('Hello!')
错误信息:
2017-06-14 11:37:09 [CELL/0] OUT Starting health monitoring of container 2017-06-14 11:37:09 [APP/PROC/WEB/0] OUT Hello! 2017-06-14 11:37:09 [APP/PROC/WEB/0] OUT Exit status 0 2017-06-14 11:37:09 [CELL/0] OUT Exit status 143 2017-06-14 11:37:09 [CELL/0] OUT Destroying container 2017-06-14 11:37:09 [API/0] OUT Process has crashed with type: "web" 2017-06-14 11:37:09 [API/0] OUT App instance exited with guid 6fdede46-6751-4725-ad78-b76262dbe701 payload: {"instance"=>"", "index"=>0, "reason"=>"CRASHED", "exit_description"=>"2 error(s) occurred:\n\n* 2 error(s) occurred:\n\n* Codependent step exited\n* cancelled\n* cancelled", "crash_count"=>4, "crash_timestamp"=>1497433029411246770, "version"=>"98e2a035-e38f-4169-95fb-2701c8486e9c"} 2017-06-14 11:37:09 [CELL/0] OUT Successfully destroyed container 2017-06-14 11:38:31 [CELL/0] OUT Creating container
注意: python:3
的默认CMD
是python3
。
退出代码143意味着 这里提到的 SIGTERM
。 这是docker工发送的 。
所以你需要你的python3应用程序正常处理SIGTERM信号
不要忘记,你的Python应用程序,一旦完成并退出主函数,将导致容器自动停止并退出。
OP 在评论中增加了 :
与此同时,我发现在Docker环境中处理
SIGTERM
是完美的。但是,在CloudFoundry中使用相同的代码不会阻止容器崩溃。
在CloudFoundry中,您需要一个始终运行的应用程序,而不仅仅是执行任务,然后像脚本一样停止。
即使停止没有错误,在CloudFoundry中被检测为崩溃。我使用瓶框架将脚本转换为REST服务器。 现在它一直在运行,但只有在通过它的url调用时才能完成任务。