docker stop 與 docker kill的區別

振宇要低調發表於2016-03-16

 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訊號通知任務。

 

相關文章