docker stop 與 docker kill的區別
docker stop 與 docker kill 均可以將容器停掉,但二者究竟有什么區別呢?首先,摘錄一下官網對這兩個功能的描述:
docker stop: Stop a running container (send SIGTERM, and then SIGKILL after grace period) [...] The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. [emphasis mine]
docker kill: Kill a running container (send SIGKILL, or specified signal) [...] The main process inside the container will be sent SIGKILL, or any signal specified with option --signal. [emphasis mine]
docker stop,支持“優雅退出”。先發送SIGTERM信號,在一段時間之后(10s)再發送SIGKILL信號。Docker內部的應用程序可以接收SIGTERM信號,然后做一些“退出前工作”,比如保存狀態、處理當前請求等。
docker kill,發送SIGKILL信號,應用程序直接退出。
線上應用優雅退出十分必要。docker stop也不是docker獨有的設計,lxc和google borg系統都有類似設計,即在發送SIGKILL之前,發送SIGTERM信號通知任務。