Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run -it over ssh: the input device is not a TTY

Tags:

docker

ssh

I'd like to run a docker container interactively directly from an ssh command.

My current code appears as follows (I'm using echo hi as a placeholder):

$ ssh [email protected] -p 2222 "docker run -ti ubuntu:xenial echo hi"
the input device is not a TTY

I've also tried explicitly making an interactive login shell:

$ ssh [email protected] -p 2222 "bash -ilc 'docker run -ti ubuntu:xenial'"
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
the input device is not a TTY

If I ssh and interactively run the command, it works great:

$ ssh [email protected] -p 2222
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-34-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
----------------------------------------------------------------
  Ubuntu 14.04.5 LTS                          built 2016-08-26
----------------------------------------------------------------
Last login: Mon Oct 30 23:25:35 2017 from 10.0.2.2
vagrant@vagrant:~$ docker run -ti ubuntu:xenial echo hi
hi

What am I missing here?

like image 585
Anthony Sottile Avatar asked Oct 30 '17 23:10

Anthony Sottile


1 Answers

SSH does not set up a TTY by default when an explicit command is passed on the argument list (as opposed to when running a remote interactive shell as a default operation). To work around this, add -tt:

$ ssh -tt [email protected] -p 2222 "docker run -ti ubuntu:xenial echo hi"

A single -t will set up a remote TTY if and only if a local TTY is present; -tt does so unconditionally.

The RequestTTY option can also be set explicitly, either on the command line:

$ ssh -o 'RequestTTY force' [email protected] -p 2222 \
>   'docker run -ti ubuntu:xenial echo hi'

...or in ~/.ssh/config:

Host vagrant
    HostName 127.0.0.1
    Port 2222
    User vagrant
    RequestTTY force

...used as:

ssh vagrant 'docker run -ti ubuntu:xenial echo hi'

Quoting the man page for ssh:

-t - Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Quoting the ssh_config man page:

RequestTTY - Specifies whether to request a pseudo-tty for the session. The argument may be one of: no (never request a TTY), yes (always request a TTY when standard input is a TTY), force (always request a TTY) or auto (request a TTY when opening a login session). This option mirrors the -t and -T flags for ssh(1).

like image 142
Charles Duffy Avatar answered Nov 09 '22 21:11

Charles Duffy