moby: Docker Exec does not resize terminal

I noticed this on both master (and docker 17.11-rc2, both on Ubuntu and Docker for Mac);

Client:
 Version:      17.11.0-ce-rc2
 API version:  1.34
 Go version:   go1.8.3
 Git commit:   d7062e5
 Built:        Wed Nov  1 22:11:59 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.11.0-ce-rc2
 API version:  1.34 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   d7062e5
 Built:        Wed Nov  1 22:10:29 2017
 OS/Arch:      linux/amd64
 Experimental: false

When running docker exec, the terminal doesn’t resize:

$ docker run -d --name foo nginx:alpine
$ docker exec -it foo sh
/ # 0123456789012345678901234567890123456789012345678901234567890123456789012345
6789

Checking the size with stty size does show the correct dimensions:

/ # stty size
93 410

This only happens on docker exec; doing docker run -it works correct:

$ docker run -it --rm nginx:alpine sh
/ # 01234567890123456789012345678901234567890123456789012345678901234567890123456789

(and shows the same output for stty size):

/ # stty size
93 410

Checking the daemon logs, the resize parameters are passed to the API for both docker run and docker exec;

time="2017-11-04T00:17:19.801321056Z" level=debug msg="Calling POST /v1.34/containers/378120448a6af3b289c12574445dde6f7a5a8f6fb09a311a46d1ffc735fab010/resize?h=93&w=410"


time="2017-11-04T00:16:37.697677138Z" level=debug msg="Calling POST /v1.34/exec/c57124724228e4af259281af94206ce9f02c64e472408198f665b342c1347f03/resize?h=93&w=410"

ping @vieux @mlaventure @crosbymichael

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 24
  • Comments: 31 (14 by maintainers)

Commits related to this issue

Most upvoted comments

So, this still seems to be an issue. My workaround for now is setting the COLUMNS and LINES environment variables in the Exec.

docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` shelltest bash

The suggestion of resizing the window is an interesting one. It makes it seem that the SIGWINCH signal propagates the terminal size correctly. A small test …

Start a TTY via docker exec:

docker exec -ti shelltest bash
$ stty size
0 0

On the host, send a SIGWINCH to the docker exec process:

kill -SIGWINCH $(ps -ef | grep 'docker exec -ti shelltest' | grep -v grep | awk '{ print $2}')

Re-run stty in the same TTY as the first step:

$ stty size
68 212

It seems that an initial SIGWINCH on the start of the TTY is missed or isn’t sent (correctly).

This indeed seems an issues since the early 17.11 releases. It’s quite easy to reproduce:

  • make sure your terminal is > 80x24 chars.
  • use docker engine 17.11 (eg. 17.11.0-ce)
  • start a container /w tty +interactive: docker run -ti --name shelltest ubuntu bash
  • start top and verify it’s in full screen/terminal
  • open a new terminal and make sure your terminal is > 80x24 chars.
  • run docker exec -ti shelltest bash
  • start top and see it starts in a 80x24 boundary

No issue in =< 17.10.

Yes, sorry this is still in my list. Unfortunately, I still haven’t gotten around setting up a proper working env for docker-ce (the mono-repo doesn’t make it easy 😅). I’ll try to dedicate some time to it in the next couple weeks.

The SIGWINCH workaround, suggested by @mpepping is much better than setting COLUMNS and LINES envs because it also supports resizing of the terminal window, without the need to restart Docker exec every time your terminal size changes. However the issue was that it had to be manually run every time after docker exec, so here’s my workaround:

If you use Makefile, you can add:

.PHONY: sh
sh:
#	Fix Docker terminal to resize properly:
#	https://github.com/moby/moby/issues/35407#issuecomment-357046159
	@./fix-docker-terminal.bash &
	@docker exec -it -u www-data container zsh

and create fix-docker-terminal.bash in the same directory:

#!/bin/bash
sleep 1
ps -ef | grep 'docker exec -it' | grep -v grep | awk '{ print $2}' | xargs kill -SIGWINCH

Do not forget to chmod +x fix-docker-terminal.bash.

If you don’t use Makefile, you can go with this one line: ./fix-docker-terminal.bash & docker exec -it -u www-data container zsh

@thaJeztah Thanks! This will probably make a lot of people happy! 👍

Any updates here? @mlaventure Three months since your comment, did you find time to fix this?

Yes, it looks like a race condition, and likely the initial resize is triggered to early; cli/command/container/tty.go#L48. Moving the resize to the end of that function (or adding another resize after monitoring is started; cli/command/container/exec.go#L156-L160) seems to help slightly, but it’s still racy.

So, this may actually be the same issue as https://github.com/moby/moby/issues/31223

ping @mlaventure

I am getting this issue again on a fresh docker installation:

pi@rpi-dev:~ $ docker run -ti hello-world failed to resize tty, using default size

pi@rpi-dev:~ $ docker --version Docker version 19.03.12, build 48a6621

This is a fresh docker installation on a raspberry pi (raspbian). Any help would be greatly appreciated. TIA.