【发布时间】: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
【问题讨论】:
-
据我了解,其中一个答案链接到他们建议使用脚本的一些示例。这就是我正在做的,但是容器崩溃了。
-
崩溃是什么意思?您收到什么输出和日志?
-
已经解决了!谢谢!
标签: docker dockerfile docker-entrypoint