Docker 终端中的 Docker 行数在 docker 内更改 [英] Docker number of lines in terminal changing inside docker

查看:54
本文介绍了Docker 终端中的 Docker 行数在 docker 内更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何更改以下行为.假设我的终端有 28 行.然后我使用以下命令:

$ tput lines # 我的终端28$ docker run --rm -it ubuntu:16.04 tput lines # docker 容器24 ## 为什么?$ docker run --rm -it ubuntu:16.04 bash # docker 容器内的命令root@810effa2777c:/# tput 行28

如您所见,即使所有结果都应该是 28,当我将容器调用为 docker run --rm -it ubuntu:16.04 tput lines 不管我的终端有多大,它总是给我 24.这不仅适用于 ubuntu 容器,我还尝试使用 debian (docker run --rm -it debian tput lines) 并且我得到了相同的结果 24.

这样做的目的是使用

有人知道这可能是什么以及如何解决吗?

解决方案

2018 年 9 月更新:检查 docker 18.06 是否有同样的问题 (它不应该,在moby/moby 问题 33794,还有 moby/moby 问题 35407PR 37172,部分18.06 发行说明).

<小时>

2016 年:

Ubuntu Dockerfile 包括:

CMD ["/bin/bash"]

这意味着默认的 ENTRYPOINTsh -c(我怀疑 tput linesh 会话,因为 tput 使用 terminfo 数据库,这可能是仅为该图像中的 bash 设置)

您可以尝试用 bash -c 覆盖 ENTRYPOINT 并检查是否效果更好.

虽然这在命令行中不起作用:

docker run --entrypoint/bin/bash --rm -it ubuntu:16.04 -i -c 'tput lines'24

我会检查定义自定义图像的选项.

来自 ubuntu:16.04入口点 ["/bin/bash", "-c"]

结果是一样的:

docker run --rm -it u 'tput lines'24

然而这有效":

来自 ubuntu:16.04入口点 [ "/bin/bash" ]

与:

docker@default:/c/Users/vonc/prog/testsu$ docker run --rm -it u -i -c 'ls;tput 行'bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var48

可能存在同步问题,因为同一命令不时返回 24.

实际上,以下总是返回not 24":

来自 ubuntu:16.04入口点 [ "/bin/bash", "-l", "-i", "-c" ]docker run --rm -it u -c 'sleep 0.1;ls;tput 行'48

OP silgon 提出了 在评论中:

docker run --rm -it --entrypoint/bin/bash ubuntu:16.04 -c "sleep 0.1 && tput lines"

<小时>

作为 BMitch 评论 下面:

<块引用>

鉴于 sleep 的成功,我怀疑 docker 使用 running 命令启动了容器,一旦启动,客户端就会附加到正在运行的容器.通常需要几毫秒的时间.

这给了我另一个想法:

docker@default:/c/Users/vonc/prog/testsu$docker run --entrypoint='/bin/bash' --name ub -d -it ubuntu:16.040d9b8783afbb5e3ff4232da071d3f357985351ea1ce4d142bf6617ac456fb76bdocker@default:/c/Users/vonc/prog/testsu$d 附加 ubroot@0d9b8783afbb:/# tput 行48root@0d9b8783afbb:/# exit出口docker@default:/c/Users/vonc/prog/testsu$ drmae0d9b8783afbb5e3ff4232da071d3f357985351ea1ce4d142bf6617ac456fb76b

附加会话中的 tput 行 工作正常.
(关于 drmae 别名,请参阅如何删除旧的和未使用的 Docker 映像")

<小时>

thajeztah 添加了 在评论中:

<块引用>

创建容器,然后以默认值 (80x24) 启动,然后(当 -it 时),附加会话.
会话指定终端的大小;

参见调整容器 TTY" API.

 DEBU[0244] 调用 POST/v1.25/containers/c42fd5c4eb79c06fd7f9912b8359022f7d93887afbb33b57a67ed8bb7bfee4‌ 3a/resize?h=426

有关更多信息,请参阅 docker 问题 25450.
它与 issue 10341容器创建或启动应接受高度/宽度参数"有关.Aleksa Sarai (cyphar) 补充道(2016 年 9 月):

<块引用>

这实际上在运行时规范中再次出现(opencontainers/runtime-spec PR563).
基本上,由于 Windows 需要在第一次启动时设置控制台大小的能力,我们最终可能会为所有平台添加它.

<小时>

OP silgon 指出了api/client/container/run.go:

//在启动期间告诉 Windows 守护进程 tty 的初始大小//更好的用户体验,而不是依赖后续的调整大小//导致事情赶上.如果 runtime.GOOS == "windows" {hostConfig.ConsoleSize[0],hostConfig.ConsoleSize[1] = dockerCli.GetTtySize()}

逻辑问题:

<块引用>

在 Linux 上也使用此属性并使用该值设置初始控制台大小是否有意义?

Kenfe-Mickaël Laventure (mlaventure) 就在上面,还有一个新补丁可以使其成为 Docker 1.13.

I would like to know how to change the following behavior. Let's say my terminal has 28 lines. Then I use the following commands:

$ tput lines # my terminal
28
$ docker run  --rm  -it ubuntu:16.04 tput lines  # docker container
24  ## WHY??
$ docker run  --rm  -it ubuntu:16.04 bash # docker container inside command
root@810effa2777c:/# tput lines
28

As you can see, even when all the results should be 28, when I'm calling the container as docker run --rm -it ubuntu:16.04 tput lines it always gives me 24 despite the size of my terminal. This is not only with the ubuntu container, I also tried with debian (docker run --rm -it debian tput lines) and I'm having the same result 24.

The purpose of this is to use the mdp presentation tool which takes into account the lines in your terminal. When my implementation failed, I tried some other person's docker implementation but I ran to the same error.

Here's my error in an image:

Does anyone has any idea what it could be and how can this be solved?

解决方案

Update Sept. 2018: check if docker 18.06 has the same issue (it should not, after moby/moby issue 33794, and also moby/moby issue 35407 and PR 37172, part of the 18.06 release notes).


2016:

The Ubuntu Dockerfile includes:

CMD ["/bin/bash"]

That means the default ENTRYPOINT is sh -c (and I doubt tput line works well in a sh session, since tput uses terminfo database, which might be set only for bash in that image)

You could try overwrite ENTRYPOINT with bash -c and check if that works better.

That does not work from command line though:

docker run --entrypoint /bin/bash --rm  -it ubuntu:16.04 -i -c 'tput lines'
24

I will check the option of defining a custom image.

FROM ubuntu:16.04
ENTRYPOINT ["/bin/bash", "-c"]

The result is the same though:

docker run --rm  -it u 'tput lines'
24

This however "works":

FROM ubuntu:16.04
ENTRYPOINT [ "/bin/bash" ]

With:

docker@default:/c/Users/vonc/prog/testsu$ docker run --rm  -it u -i -c 'ls; tput lines'
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
48

There might be a synchronization issue, as the same command does return 24 from time to time.

Actually, the following always return "not 24" with:

FROM ubuntu:16.04
ENTRYPOINT [ "/bin/bash", "-l", "-i", "-c" ]

docker run --rm  -it u -c 'sleep 0.1; ls; tput lines'
48

The OP silgon proposes in the comments:

docker run --rm -it --entrypoint /bin/bash ubuntu:16.04 -c "sleep 0.1 && tput lines"


As BMitch comments below:

Given the success of sleep my suspicion is that docker spins up the container with the running command, and once up, the client attaches to the running container. Typically something that takes milliseconds.

That gave me another idea:

docker@default:/c/Users/vonc/prog/testsu$ 
docker run --entrypoint='/bin/bash' --name ub -d -it ubuntu:16.04
  0d9b8783afbb5e3ff4232da071d3f357985351ea1ce4d142bf6617ac456fb76b
docker@default:/c/Users/vonc/prog/testsu$ 
d attach ub
  root@0d9b8783afbb:/# tput lines
  48
  root@0d9b8783afbb:/# exit
exit
docker@default:/c/Users/vonc/prog/testsu$ drmae
0d9b8783afbb5e3ff4232da071d3f357985351ea1ce4d142bf6617ac456fb76b

A tput lines within an attached session works just fine.
(On the drmae alias, see "How to remove old and unused Docker images")


thajeztah adds in the comments:

the container is created, then started with the defaults (80x24), and after that (when -it), a session is attached.
The session is specifying the size of the terminal;

See "Resize a container TTY" API.

 DEBU[0244] Calling POST /v1.25/containers/c42fd5c4eb79c06fd7f9912b8359022f7d93887afbb33b57a67ed8bb7bfee4‌​3a/resize?h=46&w=221 

For more, see docker issue 25450.
It is related to issue 10341 "Container create or start should accept height/width params". Aleksa Sarai (cyphar) adds (Sept. 2016):

This has actually popped up again inside the runtime-spec (opencontainers/runtime-spec PR 563).
Basically, since Windows requires the ability to set the console size on first start, we might end up adding it for all platforms.


The OP silgon points out to the code in api/client/container/run.go:

// Telling the Windows daemon the initial size of the tty during start makes
// a far better user experience rather than relying on subsequent resizes
// to cause things to catch up.
if runtime.GOOS == "windows" {
    hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = dockerCli.GetTtySize()
}

With the logical question:

would it make sense to use this property on Linux as well, and set the initial console size using that value?

Kenfe-Mickaël Laventure (mlaventure) is on it, and a new patch could make it to Docker 1.13.

这篇关于Docker 终端中的 Docker 行数在 docker 内更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆