【问题标题】:How can I execute a command/script after issuing "docker container stop" command?发出“docker container stop”命令后如何执行命令/脚本?
【发布时间】:2021-03-24 18:33:46
【问题描述】:

我希望容器在发出命令“docker container stop”后停止之前运行确定的命令。我找到了问题How to execute a script when I terminate a docker container,但它并没有真正起作用,因为容器在创建后一直崩溃,即使我在运行时添加了一个无限循环来执行。 我将容器运行为: docker run -ti -d --name test stop_test

可能出了什么问题?

这是我的 docker 文件:

#Starting with a ubuntu image
FROM ubuntu

#Installing all dependencies

#Copying files
COPY loop.sh .
COPY commands.sh .
COPY stop.sh .

#Running new container
RUN chmod +x commands.sh
RUN chmod +x stop.sh
CMD ["./commands.sh"]

#Adding Entrypoint
ENTRYPOINT [ "/bin/bash", "-c", "./stop.sh" ]

停止脚本是:

#!/bin/bash

#Defining cleanup procedure
cleanup() {
    echo "Container stopped. Running script..."
}

#Trapping the SIGTERM
trap 'cleanup' SIGTERM

#Execute a command
less /etc/password &

#Wait
wait $!

#Cleanup
cleanup

循环脚本:

#!/bin/bash

while :
do
 sleep 300
done

命令脚本是:

#!/bin/bash

./loop.sh

【问题讨论】:

  • 不是解决方案吗? stackoverflow.com/questions/35808251/…
  • 据我了解,其中一个答案链接到他们建议使用脚本的一些示例。这就是我正在做的,但是容器崩溃了。
  • 崩溃是什么意思?您收到什么输出和日志?
  • 已经解决了!谢谢!

标签: docker dockerfile docker-entrypoint


【解决方案1】:

问题在于 stop.sh 脚本正在执行的命令。我用无限循环脚本替换了它,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多