我试图阻止运行Nginx的Docker容器,只有在该Nginx实例的access.log中没有活动一段时间后才会停止。
是否可以从容器内停止Docker容器?我能想到的另一个解决方案是在主机操作系统上运行一个cron来检查/var/lib/docker/aufs/mnt/[container id]/
,但我计划启动大量容器,并且不希望保留ID列表。
答案 0 :(得分:3)
当容器中的主进程停止时,docker容器停止。
我设置了一个小的dockerfile和一个启动脚本,以显示它在你的情况下是如何工作的:
<强> Dockerfile 强>
FROM nginx
COPY start.sh /
CMD ["/start.sh"]
<强> start.sh 强>
#!/bin/bash
nginx &
sleep 20
# replace sleep 20 with your test of inactivity
nginx stop
构建容器,运行并测试
$ docker build -t ng .
$ docker run -d ng
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a373e721da7 ng:latest "/start.sh" 4 seconds ago Up 3 seconds 443/tcp, 80/tcp distracted_colden
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a373e721da7 ng:latest "/start.sh" 16 seconds ago Up 16 seconds 80/tcp, 443/tcp distracted_colden
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$
答案 1 :(得分:0)
您可以在该泊坞窗图像中共享您的泊坞袜,然后进行必要的操作。
在docker图像中共享docker sock,执行以下操作:
docker run -v /var/run/docker.sock:/run/docker.sock -v $(which docker):/bin/docker YOUR_IMAGE
在ENV vars中,您将获得容器ID,例如在容器echo $HOSTNAME
答案 2 :(得分:0)
我运行了一个nginx容器,然后无法再次启动它:
WITH LOG AS
(
SELECT 11 AS FINISH_POSITION
, NULL AS OFFICIAL_RATING
, '2013-09-18' :: DATE AS RATING_DATE
UNION ALL
SELECT 4 AS FINISH_POSITION
, NULL AS OFFICIAL_RATING
, '2013-11-08' :: DATE AS RATING_DATE
UNION ALL
SELECT 3 AS FINISH_POSITION
, NULL AS OFFICIAL_RATING
, '2014-02-27' :: DATE AS RATING_DATE
UNION ALL
SELECT 6 AS FINISH_POSITION
, '65' AS OFFICIAL_RATING
, '2014-04-24' :: DATE AS RATING_DATE
UNION ALL
SELECT 13 AS FINISH_POSITION
, '63' AS OFFICIAL_RATING
, '2014-05-05' :: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '59' AS OFFICIAL_RATING
, '2014-07-03' :: DATE AS RATING_DATE
UNION ALL
SELECT 2 AS FINISH_POSITION
, '65' AS OFFICIAL_RATING
, '2014-08-04' :: DATE AS RATING_DATE
UNION ALL
SELECT 12 AS FINISH_POSITION
, '68' AS OFFICIAL_RATING
, '2014-10-28' :: DATE AS RATING_DATE
UNION ALL
SELECT 9 AS FINISH_POSITION
, '62' AS OFFICIAL_RATING
, '2014-12-09' :: DATE AS RATING_DATE
UNION ALL
SELECT 5 AS FINISH_POSITION
, '65' AS OFFICIAL_RATING
, '2015-01-08' :: DATE AS RATING_DATE
UNION ALL
SELECT 7 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2015-01-29' :: DATE AS RATING_DATE
UNION ALL
SELECT 11 AS FINISH_POSITION
, '65' AS OFFICIAL_RATING
, '2015-04-08' :: DATE AS RATING_DATE
UNION ALL
SELECT 5 AS FINISH_POSITION
, '63' AS OFFICIAL_RATING
, '2015-04-22' :: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2015-07-14' :: DATE AS RATING_DATE
UNION ALL
SELECT 9 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2015-08-04' :: DATE AS RATING_DATE
UNION ALL
SELECT 8 AS FINISH_POSITION
, '65' AS OFFICIAL_RATING
, '2016-01-22' :: DATE AS RATING_DATE
UNION ALL
SELECT 4 AS FINISH_POSITION
, '52' AS OFFICIAL_RATING
, '2016-02-26' :: DATE AS RATING_DATE
UNION ALL
SELECT 7 AS FINISH_POSITION
, '50' AS OFFICIAL_RATING
, '2016-03-31' :: DATE AS RATING_DATE
UNION ALL
SELECT 8 AS FINISH_POSITION
, '48' AS OFFICIAL_RATING
, '2016-04-25' :: DATE AS RATING_DATE
UNION ALL
SELECT 4 AS FINISH_POSITION
, '56' AS OFFICIAL_RATING
, '2016-07-12' :: DATE AS RATING_DATE
UNION ALL
SELECT 8 AS FINISH_POSITION
, '47' AS OFFICIAL_RATING
, '2016-09-23 ' :: DATE AS RATING_DATE
UNION ALL
SELECT 9 AS FINISH_POSITION
, '52' AS OFFICIAL_RATING
, '2017-06-20' :: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '50' AS OFFICIAL_RATING
, '2017-07-04' :: DATE AS RATING_DATE
UNION ALL
SELECT 3 AS FINISH_POSITION
, '55' AS OFFICIAL_RATING
, '2017-07-20' :: DATE AS RATING_DATE
UNION ALL
SELECT 3 AS FINISH_POSITION
, '48' AS OFFICIAL_RATING
, '2017-07-29' :: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '58' AS OFFICIAL_RATING
, '2017-08-15' :: DATE AS RATING_DATE
UNION ALL
SELECT 2 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2017-08-20' :: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '48' AS OFFICIAL_RATING
, '2017-08-22 ' :: DATE AS RATING_DATE
UNION ALL
SELECT 9 AS FINISH_POSITION
, '52' AS OFFICIAL_RATING
, '2017-08-27' :: DATE AS RATING_DATE
UNION ALL
SELECT 2 AS FINISH_POSITION
, NULL AS OFFICIAL_RATING
, '2017-09-05':: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '48' AS OFFICIAL_RATING
, '2017-09-17':: DATE AS RATING_DATE
UNION ALL
SELECT 1 AS FINISH_POSITION
, '51' AS OFFICIAL_RATING
, '2017-09-21':: DATE AS RATING_DATE
UNION ALL
SELECT 11 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2017-09-25':: DATE AS RATING_DATE
UNION ALL
SELECT 13 AS FINISH_POSITION
, '63' AS OFFICIAL_RATING
, '2017-09-30':: DATE AS RATING_DATE
UNION ALL
SELECT 14 AS FINISH_POSITION
, '61' AS OFFICIAL_RATING
, '2017-10-04':: DATE AS RATING_DATE
UNION ALL
SELECT 7 AS FINISH_POSITION
, '49' AS OFFICIAL_RATING
, '2017-10-05':: DATE AS RATING_DATE
UNION ALL
SELECT 9 AS FINISH_POSITION
, NULL AS OFFICIAL_RATING
, '2017-10-09':: DATE AS RATING_DATE
UNION ALL
SELECT 11 AS FINISH_POSITION
, '60' AS OFFICIAL_RATING
, '2017-10-15':: DATE AS RATING_DATE
)
SELECT T3.FINISH_POSITION AS FINISH_POSITION
, T3.OFFICIAL_RATING AS OFFICIAL_RATING
, T3.DATE AS DATE
, CASE WHEN T3.RATING_OF_1 IS NULL OR T3.OFFICIAL_RATING IS NULL THEN 0
ELSE CAST(T3.RATING_OF_1 AS INT) - CAST(T3.OFFICIAL_RATING AS INT)
END AS DIFFERENCE
FROM
(
SELECT L.FINISH_POSITION AS FINISH_POSITION
, L.OFFICIAL_RATING AS OFFICIAL_RATING
, L.RATING_DATE AS DATE
, L.OFFICIAL_RATING AS RATING_OF_1
FROM LOG AS L
WHERE L.RATING_DATE < (SELECT MIN(RATING_DATE)
FROM LOG
WHERE FINISH_POSITION = 1)
UNION
SELECT L.FINISH_POSITION AS FINISH_POSITION
, L.OFFICIAL_RATING AS OFFICIAL_RATING
, L.RATING_DATE AS DATE
, L.OFFICIAL_RATING AS RATING_OF_1
FROM LOG AS L
WHERE OFFICIAL_RATING IS NULL
UNION
SELECT L.FINISH_POSITION AS FINISH_POSITION
, L.OFFICIAL_RATING AS OFFICIAL_RATING
, L.RATING_DATE AS DATE
, T2.RATING_OF_1 AS RATING_OF_1
FROM LOG AS L
LEFT JOIN
(
SELECT T1.OFFICIAL_RATING_OF_1 AS RATING_OF_1
, T1.DATE AS DATE
, LEAD(T1.DATE) OVER (ORDER BY T1.DATE ASC) AS NEXT_RATEING_OF_1
FROM (
SELECT FINISH_POSITION
, OFFICIAL_RATING AS OFFICIAL_RATING_OF_1
, RATING_DATE AS DATE
FROM LOG
WHERE FINISH_POSITION = 1
) AS T1
WHERE T1.OFFICIAL_RATING_OF_1 IS NOT NULL
) AS T2
ON L.RATING_DATE > T2.DATE AND L.RATING_DATE < T2.NEXT_RATEING_OF_1
WHERE OFFICIAL_RATING IS NOT NULL AND T2.NEXT_RATEING_OF_1 IS NOT NULL
UNION
SELECT FINISH_POSITION AS FINISH_POSITION
, OFFICIAL_RATING AS OFFICIAL_RATING
, RATING_DATE AS DATE
, OFFICIAL_RATING AS OFFICIAL_RATING_OF_1
FROM LOG
WHERE FINISH_POSITION = 1
) AS T3
UNION
SELECT L.FINISH_POSITION AS FINISH_POSITION
, L.OFFICIAL_RATING AS OFFICIAL_RATING
, L.RATING_DATE AS DATE
, CASE WHEN OFFICIAL_RATING IS NULL THEN 0
ELSE CAST(T3.RATING_OF_1 AS INT) - CAST(L.OFFICIAL_RATING AS INT)
END AS DIFFERENCE
FROM LOG AS L
RIGHT OUTER JOIN
(
SELECT DISTINCT
T2.RATING_OF_1
, T2.DATE AS DATE
, T2.NEXT_RATEING_OF_1
FROM
(
SELECT T1.OFFICIAL_RATING_OF_1 AS RATING_OF_1
, T1.DATE AS DATE
, LEAD(T1.DATE) OVER (ORDER BY T1.DATE ASC) AS NEXT_RATEING_OF_1
FROM (
SELECT FINISH_POSITION
, OFFICIAL_RATING AS OFFICIAL_RATING_OF_1
, RATING_DATE AS DATE
FROM LOG
WHERE FINISH_POSITION = 1
) AS T1
) AS T2
WHERE T2.NEXT_RATEING_OF_1 IS NULL
) AS T3
ON L.RATING_DATE > T3.DATE
ORDER BY DATE ASC
;
最简单的解决方法是“修剪”:
WITH
答案 3 :(得分:-1)
Docker可以使用exec命令在正在运行的容器中运行命令:
docker exec [-d|--detach[=false]] [--help] [-i|--interactive[=false]] [-t|--tty[=false]] CONTAINER COMMAND [ARG...]