docker run -it over ssh:输入设备不是TTY

Ant*_*ile 4 ssh docker

我想直接从ssh命令以交互方式运行docker容器.

我当前的代码如下所示(我echo hi用作占位符):

$ ssh vagrant@127.0.0.1 -p 2222 "docker run -ti ubuntu:xenial echo hi"
the input device is not a TTY
Run Code Online (Sandbox Code Playgroud)

我也尝试过明确地创建一个交互式登录shell:

$ 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
Run Code Online (Sandbox Code Playgroud)

如果我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
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

Cha*_*ffy 9

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

$ ssh -tt vagrant@127.0.0.1 -p 2222 "docker run -ti ubuntu:xenial echo hi"
Run Code Online (Sandbox Code Playgroud)

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

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

$ ssh -o 'RequestTTY force' vagrant@127.0.0.1 -p 2222 \
>   'docker run -ti ubuntu:xenial echo hi'
Run Code Online (Sandbox Code Playgroud)

......或在~/.ssh/config:

Host vagrant
    HostName 127.0.0.1
    Port 2222
    User vagrant
    RequestTTY force
Run Code Online (Sandbox Code Playgroud)

...用作:

ssh vagrant 'docker run -ti ubuntu:xenial echo hi'
Run Code Online (Sandbox Code Playgroud)

引用手册页ssh:

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

引用ssh_config手册页:

RequestTTY - 指定是否为会话请求伪tty.参数可以是以下之一:( no从不请求TTY),yes (当标准输入是TTY时始终请求TTY),force (始终请求TTY)或auto (在打开登录会话时请求TTY).此选项镜像ssh(1)的-t和-T标志.

  • 陛下,您是一位救星。 (3认同)