docker run -it ssh:input设备不是TTY

我想通过ssh命令直接交互式运行docker container。

我现在的代码如下所示(我使用echo hi作为占位符):

 $ ssh vagrant@127.0.0.1 -p 2222 "docker run -ti ubuntu:xenial echo hi" the input device is not a TTY 

我也试着明确地做一个交互式的loginshell:

 $ ssh vagrant@127.0.0.1 -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 

如果我SSH和交互式运行命令,它的工作原理:

 $ ssh vagrant@127.0.0.1 -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 

我在这里错过了什么?

在参数列表上传递显式命令时,SSH不会默认设置TTY(与运行远程交互式shell作为默认操作相反)。 要解决这个问题,请添加-tt

 $ ssh -tt vagrant@127.0.0.1 -p 2222 "docker run -ti ubuntu:xenial echo hi" 

当且仅当本地TTY存在时,单个-t将设置远程TTY; -tt无条件地这样做。

RequestTTY选项也可以在命令行中显式设置:

 $ ssh -o 'RequestTTY force' vagrant@127.0.0.1 -p 2222 \ > 'docker run -ti ubuntu:xenial echo hi' 

…或者在~/.ssh/config

 Host vagrant HostName 127.0.0.1 Port 2222 User vagrant RequestTTY force 

用作:

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

引用ssh的手册页 :

-t – 强制伪terminal分配。 这可以用来在远程机器上执行任意基于屏幕的程序,这可能是非常有用的,例如在实现菜单服务时。 多个-t选项强制tty分配,即使ssh没有本地tty。

引用ssh_config手册页 :

RequestTTY – 指定是否为会话请求一个伪tty。 参数可以是: no (从不请求TTY), yes (当标准input是TTY时总是请求TTY), force (总是请求TTY)或auto (在打开login会话时请求TTY)。 该选项镜像ssh(1)的-t和-T标志。