简体   繁体   English

如何从docker中检测bash中的完全交互式shell?

[英]How to detect fully interactive shell in bash from docker?

I'm wanting to detect in "docker run" whether -ti has been passed to the entrypoint script. 我想在“docker run”中检测-ti是否已传递给入口点脚本。

docker run --help for -t -i docker run --help for -t -i

-i, --interactive=false     Keep STDIN open even if not attached
-t, --tty=false             Allocate a pseudo-TTY

I have tried the following but even when tested locally (not inside docker) it didn't work and printed out "Not interactive" always. 我尝试了以下但是即使在本地测试(不在docker中)它也不起作用并且总是打印出“Not interactive”。

#!/bin/bash

[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'

entrypoint.sh: entrypoint.sh:

#!/bin/bash

set -e

if [ -t 0 ] ; then
    echo "(interactive shell)"
else
    echo "(not interactive shell)"
fi
/bin/bash -c "$@"

Dockerfile: 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"]

build the image: 建立图像:

$ docker build -t is_interactive .

run the image interactively: 以交互方式运行图像:

$ docker run -ti --rm is_interactive "/bin/bash"
(interactive shell)
root@dd7dd9bf3f4e:/$ echo something
something
root@dd7dd9bf3f4e:/$ echo $HOME
/root
root@dd7dd9bf3f4e:/$ exit
exit

run the image not interactively: 不以交互方式运行图像:

$ docker run --rm is_interactive "echo \$HOME"
(not interactive shell)
/root
$

This stackoverflow answer helped me find [ -t 0 ] . 这个stackoverflow的答案帮我找到了[ -t 0 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM