docker停止时运行脚本 [英] Run a script when docker is stopped
问题描述
我正在尝试使用 dockerfile 创建 docker 容器,其中 script-entry.sh 将在容器启动时执行,而 script-exit.sh 将在容器停止时执行.
I am trying to create docker container using dockerfile where script-entry.sh is to be executed when the containers starts and script-exit.sh to be executed when the container stops.
ENTRYPOINT 帮助解决了问题的第一部分,即 script-entry.sh 在启动时运行.
ENTRYPOINT helped to accomplish the first part of the problem where script-entry.sh runs on startup.
我将如何确保 script-exit.sh 在 docker exit/stop 上执行?
How will i make sure the script-exit.sh is executed on docker exit/stop ?
推荐答案
docker stop
向 Docker 容器内运行的主进程(入口脚本)发送 SIGTERM
信号).所以你需要一种方法来捕捉信号,然后触发退出脚本.
docker stop
sends a SIGTERM
signal to the main process running inside the Docker container (the entry script). So you need a way to catch the signal and then trigger the exit script.
请参阅 此链接了解有关说明信号捕获和示例(接近页面末尾)
See This link for explanation on signal trapping and an example (near the end of the page)
这篇关于docker停止时运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!