脚本执行完后,Docker容器不会停止并被删除

时间:2017-09-29 08:42:17

标签: php bash docker

我们有一个Docker服务器&Docker版本17.03.0-ce,构建60ccb22'。我们有大量工人,大约10人,每人都执行一项非常简单的任务,需要几秒钟才能完成并退出。我们决定每个人都要启动一个docker容器,当脚本完成时,容器会被停止并移除。更重要的是,crontabs处理运行因此,我们为每个使用标志--rm和-d实例化容器的worker创建了一个bash脚本,并且还启动了bin /文件夹中的脚本文件

#! /bin/sh
f=`basename $0` 
workerName=${f%.*} \\name of the bash script without the part after the .

//We link with the Docker host the folder of the worker and a log file that is going to be used for monitoring from outside the container.
docker run --rm -d --name $workerName -v `cat /mnt/volume-fra1-05/apps/pd-executioner/master/active_version`:/var/www/html -v /mnt/volume-fra1-06/apps/$workerName.log:/var/www/html/logs/$workerName.log iqucom/php-daemon-container php bin/$workerName

echo `date` $0 >> /var/log/crontab.log

因此,我们为每个使用标志--rm和-d实例化容器的工作者创建了一个bash脚本,并且还启动了bin /文件夹中的脚本文件。所有的工人都非常类似于结构和代码,而且非常简单,代码差别不大。但是,我们遇到了以下行为:一些容器(每次都是随机容器)拒绝停止并且即使在数小时后也会被移除。在容器内部,进程php bin/$workerName仍在使用PID 1运行。代码中没有类似无限循环可以阻止脚本完成。它随机发生,但仍无法找到模式。你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

因此,这可能是与您的PHP脚本以某种方式卡住相关的一些问题。但是,既然你确定在假设240secs之后假设超时那么你应该可以将容器命令更改为

  

docker run --rm -d --name $ workerName -v cat /mnt/volume-fra1-05/apps/pd-executioner/master/active_version:/ var / www / html -v /mnt/volume-fra1-06/apps/$workerName.log:/var/ www / html / logs / $ workerName.log iqucom / php-daemon-container timeout 240 php bin / $ workerName

这将确保任何卡住的容器在超时后退出,如果它没有自行退出

相关问题