问题描述
当我从 docker 容器中的入口点 bash 脚本 使用 su
命令执行脚本时,PID 更改(新的 PID 不同于1
) 并且无法捕获任何终止信号。
这个命令在我的主机(debian)上运行良好:
# Here I am root
su user1 -s './user.sh'
我认为主要问题是用户脚本在另一个进程中执行并且信号流中断。正如您在代码 (docker.sh) 中看到的那样,我尝试了许多命令,但没有令人满意的行为。我想避免 exec
命令,因为我需要入口点脚本来对终止信号执行许多清理任务。
root 用户如何通过在 docker 容器中正确捕获信号来运行用户脚本?
Dockerfile
FROM ubuntu:latest
STOPSIGNAL SIGTERM
workdir /app
copY docker.sh user.sh ./
CMD ["/bin/bash","-c","./docker.sh"]
docker.sh
#!/bin/bash
echo "################################################"
echo "[DOCKER] Started"
handleSignal()
{
echo "[DOCKER] Killed"
exit 0
}
trap handleSignal SIGTERM SIGINT SIGHUP
echo "[DOCKER] $$ $(whoami)"
if [ -z "$(id -u user1 2> /dev/null)" ]
then
useradd -s /bin/bash user1
echo "[DOCKER] User 'user1' created"
fi
su user1 -s './user.sh' # PID is not 1,no signal
# su user1 -c './user.sh' # PID is not 1,no signal
# su -l user1 -c './user.sh' # PID is not 1,no signal
# su user1 --session-command='./user.sh' # PID is not 1,no signal
# su -l user1 --session-command='./user.sh' # PID is not 1,no signal
# su - user1 -s './user.sh' # PID is not 1,no signal
# su -l user1 -s './user.sh' # PID is not 1,no signal
# su user1 -c 'bash -c ./user.sh' # PID is not 1,no signal
# su user1 -c 'exec bash -c ./user.sh' # PID is not 1,no signal
# runuser user1 -c './user.sh' # PID is not 1,no signal
# runuser user1 -s './user.sh' # PID is not 1,no signal
# runuser -l user1 -c './user.sh' # PID is not 1,no signal
# runuser user1 --session-command='./user.sh' # PID is not 1,no signal
# ./user.sh # PID is not 1,user is not user1,no signal
# bash -c ./user.sh # PID is not 1,no signal
# exec ./user.sh # user is not user1,only user.sh trap signal
# exec bash -c 'exec ./user.sh' # user is not user1,only user.sh trap signal
# exec su user1 -c './user.sh' # PID is not 1,only user.sh trap signal
# exec su user1 -s './user.sh' # PID is not 1,only user.sh trap signal
# exec su -l user1 -s './user.sh' # PID is not 1,only user.sh trap signal
# exec su user1 --session-command='./user.sh' # PID is not 1,only user.sh trap signal
# exec su - user1 --session-command='./user.sh' # PID is not 1,only user.sh trap signal
# exec su -l user1 --session-command='./user.sh' # PID is not 1,only user.sh trap signal
echo "[DOCKER] Stopped"
user.sh
#!/bin/bash
handleSignal()
{
echo "[USER] Kill signal received"
kill -TERM $child
}
trap handleSignal SIGTERM SIGINT SIGHUP
echo "[USER] $$ $(whoami)"
sleep 100 &
child=$!
wait $child
echo "[USER] Exited"
使用 su user1 -s './user.sh'
的日志
[DOCKER] Started
[DOCKER] 1 root
[DOCKER] User 'user1' created
[USER] 11 user1
Docker 在 10 秒超时后终止了容器。没有信号被捕获。
使用 exec su user1 -s './user.sh'
的日志
[DOCKER] Started
[DOCKER] 1 root
[DOCKER] User 'user1' created
[USER] 10 user1
[USER] Kill signal received
[USER] Exited
Session terminated,killing shell... ...killed.
容器已停止,但入口点脚本 (docker.sh) 中的信号处理程序由于 exec 未执行。
预期日志
[DOCKER] Started
[DOCKER] 1 root
[DOCKER] User 'user1' created
[USER] 1 user1
[USER] Kill signal received
[USER] Exited
[DOCKER] Killed
无论如何,如果信号被正确捕获,但用户脚本在另一个进程 (PID) 中执行,那就没问题了。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)