[英]How to detect fully interactive shell in bash from docker?
我想在“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中)它也不起作用並且總是打印出“Not interactive”。
#!/bin/bash
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
entrypoint.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
$
這個stackoverflow的答案幫我找到了[ -t 0 ]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.