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 · 23 comments

Comments

Projects
None yet
@cromat

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

This comment has been minimized.

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

This comment has been minimized.

cromat commented Jun 26, 2017

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

@jjlin

This comment has been minimized.

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

This comment has been minimized.

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 Jul 4, 2017

@PhilT

This comment has been minimized.

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

This comment has been minimized.

iamsoorena commented Mar 18, 2018

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

@borisdev

This comment has been minimized.

borisdev commented Mar 21, 2018

Thanks @PhilT.

This is what worked for me.

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

@ricardordb

This comment has been minimized.

ricardordb commented Apr 1, 2018

Thanks! That solved mine too!

@Yugloocamai

This comment has been minimized.

Yugloocamai commented Apr 5, 2018

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

@eigood

This comment has been minimized.

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

This comment has been minimized.

gitgaatachal commented Apr 13, 2018

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

@tzaspy

This comment has been minimized.

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

This comment has been minimized.

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

Fix terminal size issue when using kitchen login
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

Fix terminal size issue when using kitchen login
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

This comment has been minimized.

stephaneeybert commented May 10, 2018

@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 added a commit to wsdookadr/pg-docker-buildfarm that referenced this issue May 21, 2018

added new test for dump compatibility; other fixes
- 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)

@lmakarov lmakarov referenced this issue May 31, 2018

Merged

Release 1.9.0 #575

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

This comment has been minimized.

romulo1984 commented Jul 21, 2018

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

This comment has been minimized.

Member

thaJeztah commented Jul 21, 2018

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

@romulo1984

This comment has been minimized.

romulo1984 commented Jul 21, 2018

@thaJeztah 17.12.0-ce

@thaJeztah

This comment has been minimized.

Member

thaJeztah commented Jul 21, 2018

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

@romulo1984

This comment has been minimized.

romulo1984 commented Jul 21, 2018

@thaJeztah thanks.

@fkoester

This comment has been minimized.

fkoester commented Aug 6, 2018

I can confirm this issue is resolved with Docker 18.06.

@Grazfather

This comment has been minimized.

Grazfather commented Aug 12, 2018

@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

This comment has been minimized.

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

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment