【问题标题】:docker run -it over ssh: the input device is not a TTYdocker run -it over ssh:输入设备不是 TTY
【发布时间】:2018-04-12 01:16:14
【问题描述】:

我想直接从 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

我也尝试过明确地制作交互式登录 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

如果我使用 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

我在这里错过了什么?

【问题讨论】:

    标签: docker ssh


    【解决方案1】:

    默认情况下,当在参数列表上传递显式命令时,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'
    

    引用the man page for ssh:

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

    引用the ssh_config man page:

    RequestTTY - 指定是否为会话请求伪 tty。这 参数可能是以下之一:no(从不请求 TTY)、yes (当标准输入是 TTY 时,总是请求 TTY),force (总是请求 TTY)或auto(在打开一个 登录会话)。此选项反映了 -t 和 -T 标志 ssh(1).

    【讨论】:

    • 陛下,您是救生员。
    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2021-04-06
    • 1970-01-01
    • 2018-09-18
    • 2017-03-25
    相关资源
    最近更新 更多