Docker:优雅地停止Django服务器 [英] Docker: gracefully stop django server
问题描述
我正在使用 docker
和 docker-compose
.在 docker-compose.yml
内部,我有启动Django服务器的命令:
I'm using docker
and docker-compose
.
Inside docker-compose.yml
I have command that starts django server:
command: ["./run/web.sh"]
在json格式中,命令应在exec模式下运行.在 web.sh
内:
In json format command should run in exec mode. Inside web.sh
:
#!/usr/bin/env bash
exec python manage.py runserver
当我尝试通过 docker-compose stop
停止服务时,它等待了10秒钟(默认超时),然后终止了服务.在日志中,我发现 project_web_1已退出,代码为137
.
When I tried to stop service with docker-compose stop
it waited 10 seconds (default timeout) and then just kill service. In logs I've found project_web_1 exited with code 137
.
如何使用 docker stop
优雅地停止django runserver?
How to stop django runserver gracefully with docker stop
?
推荐答案
在您的 docker-compose.yml
文件中,将 stop_signal
设置为 SIGINT 代码>:
In your docker-compose.yml
file, set the stop_signal
to SIGINT
:
django:
build: .
command: ["./run/web.sh"]
stop_signal: SIGINT
我自己为此苦了一段时间……
I struggled with this for a while myself…
启动运行服务器时,您会注意到它显示使用CONTROL-C退出服务器".我的意思是说,Django的运行服务器被编写为处理 SIGINT
(ctrl-c),而不是 SIGTERM
(docker发送的信号).对我来说,更改Docker发送给Django的信号比尝试处理脚本中的 SIGTERM
并优雅地退出运行服务器要容易得多.
When starting the runserver you'll notice it says "Quit the server with CONTROL-C." I took this to mean that Django's runserver is written to handle SIGINT
(ctrl-c) and not SIGTERM
(the signal docker sends). For me, changing the signal Docker sends to Django was easier than trying to handle SIGTERM
in my script and exit the runserver gracefully.
请注意,这取决于您在 web.sh
脚本中使用 exec
以便信号不会被shell吞噬-但是我知道您已经那样做.
Note that this depends on using exec
in your web.sh
script so that the signals don't get eaten by the shell – but I see you're already doing that.
这篇关于Docker:优雅地停止Django服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!