Search code examples
macosdockerterminalhtop

How can I gracefully recover from an attached Docker container terminating?


Say I run this Docker command in one Terminal window:

$ docker run --name stackoverflow --rm ubuntu /bin/bash -c "sleep 5"

And before it exits I run this in a second Terminal window:

$ docker run -it --rm --pid=container:stackoverflow terencewestphal/htop

I'll successfully see htop running in the second container, displaying the bash sleep process running. So far so good.

After 5 seconds, the first container will exit with code 0. All good.

At this time, the second container will exit with code 137 (SIGILL). This also makes sense to me since the second container is just attached to the first one.

The problem is that this messes up macOS's Terminal.app's state:

  1. The Terminal's cursor disappears.
  2. Clicking the Terminal window causes mouse location characters to be entered as input.

Docker SIGILL

I'm hoping to find a way to avoid messing up Terminal.app state. Any suggestions?


Solution

  • You can't avoid such behaviour, because it is the htop duty to setup the terminal state after its termination, but it can't do it when terminated with SIGKILL. However, you can fix this terminal window yourself with the reset command, which is intended to initialize the terminal state.

    About the "attached" container:

    The --pid=container:<name> option means that the new container would be run in the PID namespace of first container and as the pid_namespaces(7) man page says:

    If the "init" process of a PID namespace terminates, the kernel terminates all of the processes in the namespace via a SIGKILL signal.