How to detect fully interactive shell in bash from docker?

I want to detect in the "docker pass" whether -ti was passed to the script entry point.

docker run --help for -t -i

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

I tried the following, but even when testing locally (not inside dockers), it didn’t work and always printed “Not interactively”.

 #!/bin/bash [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive' 
+5
source share
1 answer

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

create image:

 $ docker build -t is_interactive . 

run 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 

launch the image non-interactively:

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

This fooobar.com/questions/19265 / ... helped me find [ -t 0 ] .

+4
source

All Articles