Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect fully interactive shell in bash from docker?

Tags:

bash

docker

I'm wanting to detect in "docker run" whether -ti has been passed to the entrypoint script.

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.

#!/bin/bash

[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
like image 543
hookenz Avatar asked Jul 08 '15 00:07

hookenz


1 Answers

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

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

like image 108
Ewa Avatar answered Oct 17 '22 00:10

Ewa