我想在“docker run”中检测 -ti 是否已传递给入口点脚本。
docker run --help for -t -i
-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY
我已经尝试了以下方法,但即使在本地测试(不在 Docker 内部),它也不起作用并始终打印出“非交互”。
#!/bin/bash
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
入口点.sh:
#!/bin/bash
set -e
if [ -t 0 ] ; then
echo "(interactive shell)"
else
echo "(not interactive shell)"
fi
/bin/bash -c "$@"
Dockerfile:
FROM debian:7.8
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod 755 /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
CMD ["/bin/bash"]
构建图像:
$ docker build -t is_interactive .
交互式运行图像:
$ docker run -ti --rm is_interactive "/bin/bash"
(interactive shell)
root@dd7dd9bf3f4e:/$ echo something
something
root@dd7dd9bf3f4e:/$ echo $HOME
/root
root@dd7dd9bf3f4e:/$ exit
exit
非交互式运行图像:
$ docker run --rm is_interactive "echo \$HOME"
(not interactive shell)
/root
$
This 堆栈溢出答案 https://stackoverflow.com/questions/911168/how-to-detect-if-my-shell-script-is-running-through-a-pipe帮我找到[ -t 0 ]
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)