linux 带有docker run命令的脚本不会在sigint或sigterm上停止

2hh7jdfx  于 4个月前  发布在  Linux
关注(0)|答案(1)|浏览(55)

我有一个脚本,其中包含一个docker run命令来运行一些任意容器,包括在Jenkins上的某些测试。
问题是,如果运行sigint,脚本会进一步执行,甚至不会在sigterm上停止。

#!/bin/bash

function test (){
# Sample script to run a docker container
    docker run httpd:latest
    echo test failed
}

test

字符串
输出量:

$ ./script.sh
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Dec 01 06:10:51.034719 2023] [mpm_event:notice] [pid 1:tid 140008420530048] AH00489: Apache/2.4.58 (Unix) configured -- resuming normal operations
[Fri Dec 01 06:10:51.034842 2023] [core:notice] [pid 1:tid 140008420530048] AH00094: Command line: 'httpd -D FOREGROUND'
^C[Fri Dec 01 06:10:51.611085 2023] [mpm_event:notice] [pid 1:tid 140008420530048] AH00491: caught SIGTERM, shutting down
test failed # echo command executed


我希望脚本停止所有容器并中止脚本执行,以防止在函数中执行任何进一步的命令,包括进一步的docker run调用
我检查了陷阱命令,但它也不与sigterm。
你知道为什么它不工作,以及如何修复它吗?
这是正常工作

#!/bin/bash

function test (){
    sleep 10
    echo test failed
}

test


输出量:

$ ./script.sh
^C⏎

2admgd59

2admgd591#

我试过你的剧本,就像你说的,它永远不会结束。
原因是由于你是在前台模式下运行。我的意思是该进程是附加到主机,没有其他命令将被执行。
这是一个unix概念,不是docker的东西。查看底部的参考资料。
要修复它,请使用-d(表示分离模式)并为其分配一个名称,以便在以下时间停止或删除它:

docker run -d --name httpd_test httpd:latest

字符串
我修改了你的剧本

#!/bin/bash

function test () {
    docker run -d --name httpd_test httpd:latest
    echo "run tests"
    docker stop httpd_test
    echo "container stopped"
}

docker -v
test


和jenkins一起跑步
x1c 0d1x的数据
脚本如预期的那样结束:



更多详情:

相关问题