Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correct the terminal size of a `docker exec psql`?

docker exec -tiu postgres some_db_container psql db_name

This is such a convenient command to connect to any docker container one has access to, without having to discover the username and password used by the app to connect.

Trouble is, the TTY allocated at this point is insane. It doesn't know how big the terminal is, it doesn't make a line break when it tries to wrap the line, and it goes completely off the rails if you try to edit the line in the middle. Most of the time it is easier to write your query in a text editor and paste it in.

I thought maybe it's because no TTY was allocated when the container was made, but tty: true in the docker-compose didn't seem to have an effect.

I'm surprised I can't find even a discussion of this problem on the internet because the only other way of connecting to it is to know the IP of the container and the credentials for a user to connect with.

Is there some fix for this? I'd rather not have to dig out production passwords every time I want to investigate an issue, when I could connect as the postgres user if I could solve this problem.

like image 498
Altreus Avatar asked Feb 28 '18 17:02

Altreus


People also ask

What is the difference between docker run and Docker exec?

What's the Difference between Docker Run and Docker Exec? Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container!

What is tty in Docker exec?

The -t (or --tty) flag tells Docker to allocate a virtual terminal session within the container. This is commonly used with the -i (or --interactive) option, which keeps STDIN open even if running in detached mode (more about that later).

Does Docker Exec use SSH?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.


1 Answers

I ran into the same problem and after some researching found this answer https://github.com/moby/moby/issues/33794#issuecomment-312873988.

The trick is to tell the terminal in the container its size.

docker exec -tiu -e COLUMNS="`tput cols`" -e LINES="`tput lines`" postgres some_db_container psql db_name

This solves the issue for me.

like image 158
Frank Schalkwijk Avatar answered Oct 21 '22 08:10

Frank Schalkwijk