When I exec to /bin/bash inside docker (docker exec -ti container /bin/bash) and search recursive for a command, sometimes terminal starts to behave strange and everything messes up like in picture.
After that I have to exit from container and exec in again.
Ctrl-P/Ctrl-N behave strangely in my case and it's annoying to navigate through the shell history. It's possible that we are troubled by the same issue.
I have also noticed that navigating through history with up key sometimes reproduces this behaviour.
I frequently have terminal issues that seem to come down to Docker not figuring out what the right terminal size is initially. This seems to happen only on a fresh terminal. (This may or may not be what's causing your issue.) For example:
$ docker exec -it foo /bin/bash
foo@649fb21d747c:~$ stty size
0 0
foo@649fb21d747c:~$ reset -w
foo@649fb21d747c:~$ stty size
24 80
foo@649fb21d747c:~$ # That was still wrong. Now resize the terminal to get a SIGWINCH.
foo@649fb21d747c:~$ stty size
69 208
foo@649fb21d747c:~$ exit
exit
$ docker exec -it foo /bin/bash # Try it again.
foo@649fb21d747c:~$ stty size
69 208
foo@649fb21d747c:~$ # Doesn't happen anymore for this session.
I have found out that this bug is indeed due to terminal width and height parameters which sometimes is not provided to container with docker exec command
You can read more about it in this issue: #10341
I have resolved the bug this way:
docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -ti container bash
You can also put this command in a script and provide container name as parameter and create alias like docker-exec
Thanks @cromat! This solved an issue I was having using Docker for Windows in Bash on Windows where the output wasn't wide enough due to it not having a proper TTY. It looks like your backticks got formatted into code so here is the line again:
docker exec -t container_name /bin/bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"
for anyone coming here looking for an answer, you can find it here on stackoverflow
Thanks @PhilT.
This is what worked for me.
docker exec -it container_name sh -c "stty rows 50 && stty cols 150 && bash"
Thanks! That solved mine too!
After trying a dozen solutions @PhilT 's was the only one that worked
This is the more correct approach. No need for double-exec of bash.
docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -ti container bash
@eigood's solution worked like charm at the first try...
I use ubuntu with gnome. Usually I resize the window and the issue is resolved itself.
Resizing in gnome is Ctrl+Super+(Down Arrow)
and then Ctrl+Super+(Up Arrow)
@eigood Thank you for the correct solution. Why won't Docker just do this?
@PhilT @cromat In my case I do need the -i
option as in -it
instead of just the -t
option.
I created an alias to make my life easier.
alias dexec='docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -ti $1'
Eg.:
dexec containername_service_1 bash
@romulo1984 what version of docker are you running? this should be fixed in the current version
@thaJeztah 17.12.0-ce
That version reached EOL in March; I'd recommend updating to 18.06 (current stable release), which has the fix for this
@thaJeztah thanks.
I can confirm this issue is resolved with Docker 18.06.
@nodakai I have the same issue as you: I don't have issues with width or anything, but Docker something won't redraw. Specifically, if I use ctrl-p and ctrl-n to scroll through bash history (which is my habit) it doesn't redraw. If I use the arrow keys, it does.
Did you manage to find a way around this issue?
@Grazfather I think that's a different issue. I think the issue there is that the key sequence to detach from a container is C-p C-q
, so docker sees your C-p
and is waiting for the C-q
in order to detach. I haven't actually tried, but it could be that using --detach-keys=
could help.
for anyone coming here looking for an answer, you can find it here on stackoverflow
The answer linked to here is the best solution imo because other solutions don't work when you switch users inside the container, assuming you want the new user's environment which will overwrite the LINES and COLUMNS environment variables.
I use xshell exec in docker ..
when I resize the xshell window ...
exec tty size reset correctly ... anyone know this why , and how xshell do it ... I worked at lower docker version
Docker-compose example to solve this problem:
version: '3'
services:
php:
container_name: cmg-php
hostname: "docker-stand"
build:
context: ./
environment:
- COLUMNS=`tput cols`
- LINES=`tput lines`
Focus on environment. @cromat thanks!
@frops your solution is invalid. the environment
key is used to initialize environment at create container step, so it's not correct solution. you need env values set during container enter, not create.
besides, as I read @thaJeztah said it's fixed in 18.06
Should definitely be improved in 18.06 and up, but there are still some cases where the resize may need to be retriggered (you can do so by resizing your terminal window); there's a pull request that adds a retry-loop for those cases, but it's not merged yet; https://github.com/docker/cli/pull/1529
I'd consider setting COLUMNS
and LINES
a workaround for some situations, but not something to keep in your environment
I found the bug is still present if running with --tty
and without --interactive
.
Docker version 18.09.2, build 6247962
Example to reproduce:
FROM debian
RUN apt-get update
RUN apt-get install -y sl
CMD /usr/games/sl
Command with broken output / wrong terminal size:
docker run --rm --tty slimage
https://github.com/docker/cli/pull/1529 is not yet in `Docker 18.09
expected to be fixed in 19.03.0 (10th July 2019)
It was back ported to Docker CLI 18.09.5 through https://github.com/docker/cli/pull/1775
still presents in the latest Mac OS when compile openwrt with command:
make menuconfig
and all above workarounds do not work for the messed ncurses windows
docker version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:18:17 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.1
API version: 1.40 (minimum version 1.12)
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:17:52 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.6
GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
runc:
Version: 1.0.0-rc8
GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
docker-init:
Version: 0.18.0
GitCommit: fec3683
docker version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:18:17 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.1
API version: 1.40 (minimum version 1.12)
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:17:52 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.6
GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
runc:
Version: 1.0.0-rc8
GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
docker-init:
Version: 0.18.0
GitCommit: fec3683
I made myself a portable VIM configuration with all my fancy plugins for style and linting/fixing. Until I came up with @cromat solution I always had layout issues with VIM + NERDTree. Now it works well. Just made myself an alias (function, I like functions but same thing here).
$ vi ~/.zshrc
function vim {
docker run --env LINES=$(tput lines) --env COLUMNS=$(tput cols) # ...
}
Most helpful comment
I have found out that this bug is indeed due to terminal width and height parameters which sometimes is not provided to container with docker exec command
You can read more about it in this issue: #10341
I have resolved the bug this way:
You can also put this command in a script and provide container name as parameter and create alias like docker-exec