gpt4 book ai didi

docker - 尽管设置了 `-t 120`,Docker仍几乎立即停止关闭容器

转载 作者:行者123 更新时间:2023-12-02 19:08:08 26 4
gpt4 key购买 nike

这就是我构建docker镜像的方式:

docker build -f Dockerfile -t python-trapterm .

这就是我的运行方式:
 docker run -t python-trapterm:latest

我试图阻止它
 docker stop -t 120 sharp_jones

我希望在关闭容器之前需要2分钟的运行时间。但是它几乎立即关闭了吗?为什么?

这是容器过程的输出:
> docker run -t python-trapterm:latest
Setting traps...
* Starting NTP server ntpd [ OK ]
2017-09-10 06:43:02,501 pid 26 tid 140693944227584 INFO greatapp Registering signal handler
2017-09-10 06:43:02,502 pid 26 tid 140693944227584 INFO greatapp Hello!
2017-09-10 06:43:04,507 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
2017-09-10 06:43:06,510 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
2017-09-10 06:43:08,512 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
[snip]
2017-09-10 06:44:12,629 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
2017-09-10 06:44:14,631 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
Sending term to 26...
2017-09-10 06:44:16,537 pid 26 tid 140693944227584 INFO greatapp Handling shutdown...15
2017-09-10 06:44:16,538 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...
2017-09-10 06:44:16,539 pid 26 tid 140693944227584 INFO greatapp Handling shutdown...10
2017-09-10 06:44:16,539 pid 26 tid 140693944227584 INFO greatapp Checking every 2s...

您可以看到在06:44:16左右收到TERM信号,但是作业在06:44:14左右已经关闭,但是该过程在06:44:16之后终止。没有10秒的等待时间。

这是我的docker文件:
FROM ubuntu:16.04

RUN apt-get update --fix-missing && apt-get install -y \
git \
python \
python-pip \
ntp \
curl vim screen lsof

RUN mkdir -p /app

COPY ./app /app

CMD ["/app/start-app.sh"]

这是我的 app目录中的两个文件:

应用程序/start-app.sh
#!/bin/bash
set -eo pipefail

echo "Setting traps..."
trap 'cleanup_term' TERM
trap 'cleanup_kill' KILL

cleanup_term() {
echo "Sending term to `jobs -p`..."
kill -TERM `jobs -p`
kill -USR1 `jobs -p`
}

cleanup_kill() {
echo "Received kill..."
kill -TERM `jobs -p`
}


service ntp start

cd /app
python -m greatapp &

wait



应用程序/greatapp.py
import os
import time
import logging
import signal


LOG_FORMAT = "%(asctime)-15s pid %(process)d tid %(thread)4d %(levelname)-8s" \
" %(module)-8s %(message)s"

logging.basicConfig(format=LOG_FORMAT)
LOG = logging.getLogger(__file__)
LOG.setLevel(logging.DEBUG)

def docker_shutdown_handler(_signum, _frame):
LOG.info("Handling shutdown...%s", _signum)
# sys.exit()


LOG.info('Registering signal handler')
signal.signal(signal.SIGTERM, docker_shutdown_handler)
signal.signal(signal.SIGUSR1, docker_shutdown_handler)


def main():
LOG.info("Hello!")
sleep_time = 2
while True:
time.sleep(sleep_time)
LOG.info("Checking every %ds...", sleep_time)


if __name__ == '__main__':
main()

最佳答案

您的Shell脚本正在退出。发生这种情况时,由于是pid 1,该容器将立即退出。如果将以下内容添加到脚本顶部,则可以看到此信息:

trap 'echo Script exiting' EXIT

这很可能是bash中SIGKILL的默认操作,因为在最后一次等待之后它没有运行任何命令。解决此问题的快速方法是在清除函数中添加一个等待,以便信号处理程序挂起:
cleanup_term() {
echo "Sending term to `jobs -p`..."
kill -TERM `jobs -p`
kill -USR1 `jobs -p`
echo "waiting again"
wait
echo "cleanup_term done"
}

或者我建议只执行python脚本,以便它直接处理信号,并消除pid 1中的bash。start-app.sh如下所示:
#!/bin/bash
set -eo pipefail

service ntp start

cd /app
exec python -m greatapp

关于docker - 尽管设置了 `-t 120`,Docker仍几乎立即停止关闭容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com