Docker not responding to CTRL+C in terminal

Most likely the container image you use is not handling process signals properly. If you are authoring the image then change it as Roland Webers' answer suggests. Otherwise try to run it with --init.

docker run -it --init ....

This fixes Ctrl+C for me. Source: https://docs.docker.com/v17.09/engine/reference/run/#specify-an-init-process


This post proposes CTRL-Z as a workaround for sending the process to background and then killing the process by its process id: Cannot kill Python script with Ctrl-C

Possible problems:

  • The program catches ctrl-c and does nothing, very unlikely.

  • There are background processes that are not managed correctly. Only the main process receives the signal and sub-processes hang. Very likely what's happening.

Proposed Solution:

  • Check the programs documentation on how it's properly started and stopped. ctrl-c seems not to be the proper way.

  • Wrap the program with a docker-entrypoint.sh bash script that blocks the container process and is able to catch ctrl-c. This bash example should help: https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash

  • After catching ctrl-c invoke the proper shutdown method for ipython notebook.


The problem is that Ctrl-C sends a signal to the top-level process inside the container, but that process doesn't necessarily react as you would expect. The top-level process has ID 1 inside the container, which means that it doesn't get the default signal handlers that processes usually have. If the top-level process is a shell, then it can receive the signal through its own handler, but doesn't forward it to the command that is executed within the shell. Details are explained here. In both cases, the docker container acts as if it simply ignores Ctrl-C.

If you're building your own images, the solution is to run a minimal init process, such as tini or dumb-init, as the top-level process inside the container.