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?
The comments about
sh
versus terminfo are largely irrelevant. The relevant part (not clear in the given answer) is the way the command is executed.tput
checks three features in the following order (usingsetupterm
):TERM=xterm
, it is 24 by 80),LINES
andCOLUMNS
environment variables.A command which is run without an interactive shell could be executed in a way that precludes getting the current window size. For example, that is a feature of
ssh
(the-t
option). Also, it would be possible (though pointless) for Docker to set theLINES
andCOLUMNS
variables.Either case (1) or (3) is enough to explain the behavior; introducing time-delays and races does not do that.
It has been fixed in docker 18.06: https://github.com/moby/moby/issues/33794#issuecomment-406814439
UPDATE
you can now install
goinside
command line tool with:and go inside a docker container with a proper terminal size with:
Logic behind goinside
thanks to @VonC answer we've got a solution for this problem with a simple bash snippet that we put in
~/.profile
:now you are able to get inside a docker container without terminal size issues with:
$ goinside containername
remember to
source ~/.profile
before using thegoinside
function.enabling autocompletion in bash
(as it's shared in one of comments below) if you want to enable autocompletion for
goinside
you can use this snippet in.profile
:enabling autocompletion in zsh
if you are using
zsh
as your default terminal you can use this snippet inside your~/.zshrc
file:I just tested with version
Docker version 18.06.1-ce, build e68fc7a
. It seems to have the same problem. However, one of the guys in the github issue gave a practical workaround:Update Sept. 2018: check if docker 18.06 has the same issue (it should not, after
moby/moby
issue 33794, and alsomoby/moby
issue 35407 and PR 37172, part of the 18.06 release notes).2016:
The Ubuntu Dockerfile includes:
That means the default
ENTRYPOINT
issh -c
(and I doubttput line
works well in ash
session, since tput usesterminfo
database, which might be set only for bash in that image)You could try overwrite
ENTRYPOINT
withbash -c
and check if that works better.That does not work from command line though:
I will check the option of defining a custom image.
The result is the same though:
This however "works":
With:
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:
The OP silgon proposes in the comments:
As BMitch comments below:
That gave me another idea:
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:
See "Resize a container TTY" API.
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):
The OP silgon points out to the code in
api/client/container/run.go
:With the logical question:
Kenfe-Mickaël Laventure (
mlaventure
) is on it, and a new patch could make it to Docker 1.13.