Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange terminal behaviour inside docker #33794

Closed
cromat opened this issue Jun 23, 2017 · 34 comments
Closed

Strange terminal behaviour inside docker #33794

cromat opened this issue Jun 23, 2017 · 34 comments

Comments

@cromat
Copy link

cromat commented Jun 23, 2017

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.
selection_017

@nodakai
Copy link

nodakai commented Jun 26, 2017

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.

@cromat
Copy link
Author

cromat commented Jun 26, 2017

I have also noticed that navigating through history with up key sometimes reproduces this behaviour.

@jjlin
Copy link

jjlin commented Jun 29, 2017

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.

@cromat
Copy link
Author

cromat commented Jul 4, 2017

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

@cromat cromat closed this as completed Jul 4, 2017
@PhilT
Copy link

PhilT commented Aug 17, 2017

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"

@iamsoorena
Copy link

iamsoorena commented Mar 18, 2018

for anyone coming here looking for an answer, you can find it here on stackoverflow

@borisdev
Copy link

Thanks @PhilT.

This is what worked for me.

docker exec -it container_name sh -c "stty rows 50 && stty cols 150 && bash"

@ricardordb
Copy link

Thanks! That solved mine too!

@Yugloocamai
Copy link

After trying a dozen solutions @PhilT 's was the only one that worked

@eigood
Copy link

eigood commented Apr 12, 2018

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

@gitgaatachal
Copy link

@eigood's solution worked like charm at the first try...

@tzaspy
Copy link

tzaspy commented Apr 27, 2018

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)

@N4M3Z
Copy link

N4M3Z commented Apr 30, 2018

@eigood Thank you for the correct solution. Why won't Docker just do this?

webframp added a commit to webframp/kitchen-dokken that referenced this issue May 6, 2018
docker exec doesn't supply terminal width and height values to containers
currently. This should really be fixed upstream but this is workaround for the
issue until then. There's a 2 year old github issue for it so I doubt it's a
priority right now.

Before:
```
sme@megatron  ~  stty size
48 151

sme@megatron  ~  kitchen login

root@dokken:/# stty size
0 0
```

After:
```
sme@megatron  ~  stty size
48 151

sme@megatron  ~  kitchen login

root@dokken:/# stty size
48 151
```

refs:
- moby/moby#33794
- moby/moby#10341

Signed-off-by: Sean Escriva <sean.escriva@gmail.com>
webframp added a commit to webframp/kitchen-dokken that referenced this issue May 6, 2018
docker exec doesn't supply terminal width and height values to containers
currently. This should really be fixed upstream but this is workaround for the
issue until then. There's a 2 year old github issue for it so I doubt it's a
priority right now.

Before:
```
sme@megatron ~ stty size
48 151

sme@megatron ~ kitchen login

root@dokken:/# stty size
0 0
```

After:
```
sme@megatron ~ stty size
48 151

sme@megatron ~ kitchen login

root@dokken:/# stty size
48 151
```

refs:
- moby/moby#33794
- moby/moby#10341

Signed-off-by: Sean Escriva <sean.escriva@gmail.com>
@stephaneeybert
Copy link

@PhilT @cromat In my case I do need the -i option as in -it instead of just the -t option.

achekulaev added a commit to docksal/docksal that referenced this issue May 17, 2018
wsdookadr pushed a commit to wsdookadr/pg-docker-buildfarm that referenced this issue May 21, 2018
- added new test checking for custom format dump compatibility
- fixed terminal width and height for shell_pg and shell_root commands
  by setting those using the host terminal width and height
    see also moby/moby#33794 (comment)
estarter pushed a commit to estarter/d-tools that referenced this issue Jun 8, 2018
faiyaz7283 added a commit to faiyaz7283/dcutil that referenced this issue Jun 12, 2018
faiyaz7283 added a commit to faiyaz7283/dcutil that referenced this issue Jun 12, 2018
@romulo1984
Copy link

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

@thaJeztah
Copy link
Member

@romulo1984 what version of docker are you running? this should be fixed in the current version

@romulo1984
Copy link

@thaJeztah 17.12.0-ce

@thaJeztah
Copy link
Member

That version reached EOL in March; I'd recommend updating to 18.06 (current stable release), which has the fix for this

@romulo1984
Copy link

@thaJeztah thanks.

@fkoester
Copy link

fkoester commented Aug 6, 2018

I can confirm this issue is resolved with Docker 18.06.

@Grazfather
Copy link

@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?

@wasosa
Copy link

wasosa commented Aug 22, 2018

@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.

@dramzy
Copy link

dramzy commented Dec 3, 2018

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.

@gaopeiliang
Copy link

I use xshell exec in docker ..

image

when I resize the xshell window ...
image

exec tty size reset correctly ... anyone know this why , and how xshell do it ... I worked at lower docker version

image

@frops
Copy link

frops commented Feb 1, 2019

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!

@glensc
Copy link
Contributor

glensc commented Feb 2, 2019

@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

@thaJeztah
Copy link
Member

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; docker/cli#1529

I'd consider setting COLUMNS and LINES a workaround for some situations, but not something to keep in your environment

@mviereck
Copy link

mviereck commented Mar 24, 2019

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

@thaJeztah
Copy link
Member

docker/cli#1529 is not yet in `Docker 18.09

@nidash
Copy link

nidash commented Jul 4, 2019

expected to be fixed in 19.03.0 (10th July 2019)

@thaJeztah
Copy link
Member

It was back ported to Docker CLI 18.09.5 through docker/cli#1775

@moralrebuild
Copy link

moralrebuild commented Aug 19, 2019

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

@aminnairi
Copy link

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) # ... 
}

@acataluddi
Copy link

If the detection of the windows size fails in a BASH script and you're getting the default 80 columns, try the following:

docker exec -e COLUMNS="$(stty size | awk '{print $2}')" -e LINES="$(stty size | awk '{print $1}')" \
       -it container //bin/bash

Regards,
Adriano C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests