25

Having an issue with Docker at the moment; I'm using it to run an image that launches an ipython notebook on startup. I'm looking to make some edits to ipython notebook itself, so I need to close it after launch.

However, hitting CTRL+C in the terminal just inputs "^C" as a string. There seems to be no real way of using CTRL+C to actually close the ipython notebook instance.

Would anyone have any clues as to what can cause this, or know of any solutions for it?

Eoghan
  • 1,720
  • 2
  • 17
  • 35

5 Answers5

37

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

gertas
  • 16,869
  • 1
  • 76
  • 58
9

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.

Roland Weber
  • 1,865
  • 2
  • 17
  • 27
6

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.

Community
  • 1
  • 1
blacklabelops
  • 4,708
  • 5
  • 25
  • 42
4

From this post on the Docker message boards:

Open a new shell and execute

$ docker ps # get the id of the running container
$ docker stop <container> # kill it (gracefully)

This worked well for me. CTRL-Z, CTRL-\, etc. only came up as strings, but this killed the Docker container and returned the tab to terminal input.

WhooNo
  • 911
  • 2
  • 12
  • 28
1

@maybeg's answer already explains very well why this might be happening.

Regarding stopping the unresponsive container, another solution is to simply issue a docker stop <container-id> in another terminal. As opposed to CTRL-C, docker stop does not send a SIGINT but a SIGTERM signal, to which the process might react differently.

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop a running container by sending SIGTERM and then SIGKILL after a grace period

If that fails, use docker kill <container-id> which sends a SIGKILL immediately.

helmbert
  • 35,797
  • 13
  • 82
  • 95
  • Do you have a best-practise solution or entrypoint script on how to wrap processes that require proper shutdown routines in docker? – blacklabelops Jul 11 '15 at 09:58