当我在控制台上输入时我想要:
docker ^a docker container^ stop
在终止之前执行脚本.那可能吗?
最佳答案
The docker stop command attempts to stop a running container first by sending a SIGTERM signal to the root process (PID 1) in the container. If the process hasn’t exited within the timeout period a SIGKILL signal will be sent.
实际上,这意味着您必须定义一个ENTRYPOINT脚本,该脚本将拦截(陷阱)SIGTERM信号并根据需要执行任何关闭逻辑.
示例入口点脚本可能如下所示:
#!/bin/bash
#Define cleanup procedure
cleanup() {
echo "Container stopped,performing cleanup..."
}
#Trap SIGTERM
trap 'cleanup' SIGTERM
#Execute a command
"${@}" &
#Wait
wait $!
(shell信号处理,关于等待,更详细解释here)
请注意,使用上面的入口点,只有在显式停止容器时才会执行清理逻辑,如果您希望它在基础进程/命令自行停止(或失败)时也运行,则可以按如下方式对其进行重组.
...
#Trap SIGTERM
trap 'true' SIGTERM
#Execute command
"${@}" &
#Wait
wait $!
#Cleanup
cleanup
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。