0

I am running Docker Desktop 3.5.1 on MacOS Big Sur and I am totally confused about the following behaviour:

If I run docker run -it --rm postgres psql --help I get the psql usage information (all as expected) and I can continue to run commands in my terminal. Edit to clarify: the docker container exits and terminates as expected, but my zsh session remains active (also as expected).

However, if I run psql with an invalid flag, say, docker run -it --rm postgres psql -m then I get

/usr/lib/postgresql/13/bin/psql: invalid option -- 'm'
Try "psql --help" for more information.

[Process completed]

and my terminal session exits. Edit to clarify: the docker container exits as expected, but it takes the host zsh session with it (unexpected).

What I'm trying to work out is why does my terminal session exit and how can I avoid this happening?

Jarym
  • 2,046
  • 1
  • 15
  • 13
  • How exactly did you run `docker run ...`? I would be very surprised if this happened when you simply type the command at the shell prompt. (Or did you execute `set -e` in your shell session prior to running `docker run ...`?) – chepner Jul 03 '21 at 15:08

2 Answers2

0

To keep a session open you can execute bash like this:

docker run --rm -it postgres /bin/bash

Then you can run as many psql commands as you like and it wont exit unless bash exits.

edit:

It seems terminal closing behaviour can be configured in OS

https://stackoverflow.com/a/17910412/657477

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • I am confused why @Jarym said `docker run -it --rm postgres psql --help` keeps his terminal session active. It should still exit the session since `psql --help` also exits with `0` exit code. – Eranga Heshan Jul 03 '21 at 02:22
  • You are right, I wrongly assumed it was opening a terminal session or something but I just tried and it also returns exit code. – Guerrilla Jul 03 '21 at 02:26
  • I don't necessarily want a bash session in my docker container, I just don't want my Mac Terminal zsh session ending when an issued 'docker run psql' (with an invalid option) terminates. – Jarym Jul 03 '21 at 02:29
  • The mac terminal might be reading the error code, I am unable to test that unfortunately. Docker is always exiting on every command regardless. – Guerrilla Jul 03 '21 at 02:37
  • 1
    When you execute an invalid command in the docker interactive session, does it end the docker interactive session and your zsh terminal session both? If so, check whether you have any special configuration in your zsh. Try to run an invalid command `psql -m` in your zsh terminal (not inside the docker) and see if it still exits – Eranga Heshan Jul 03 '21 at 02:39
  • @ErangaHeshan - "When you execute an invalid command in the docker interactive session, does it end the docker interactive session and your zsh terminal session both?" - correct, precisely. I don't have psql installed on my Mac, I was hoping to just be able to run it inside Docker. – Jarym Jul 03 '21 at 02:43
  • 1
    @Jarym That is not a problem. I just wanted to make sure you can run an invalid command in your zsh terminal and still keep the terminal session running. So even though you don't have `psql` installed, you can try `psql -m` because that command will be invalid anyway. – Eranga Heshan Jul 03 '21 at 02:46
  • Check out link I added. Seems closing on error is configurable – Guerrilla Jul 03 '21 at 02:50
  • @ErangaHeshan - "check whether you have any special configuration in your zsh" -- that was the magic that helped me identify the problem. I had some garbage (a source someotherfile line) in my ~/.zprofile. As soon as I removed that psql in docker works for both --help and -m without my zsh session terminating. – Jarym Jul 03 '21 at 02:54
0

Very weird behaviour but @ErangaHeshan's comments pointed me to some nonsense inside my .zprofile file. As soon as that was commented out then psql in docker stopped taking down my host zsh session on exit.

Jarym
  • 2,046
  • 1
  • 15
  • 13