Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker not responding to CTRL+C in terminal

Tags:

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?

like image 763
Eoghan Avatar asked Jul 10 '15 21:07

Eoghan


People also ask

What does Ctrl C do in docker?

But, if we've launched our container without the -d or -it option, the CTRL-c command stops the container instead of disconnecting from it.

How do I stop docker Ctrl C?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output. For more details, see the official docker documentation. --name youtrack-server-instance — the arbitrary name for the container.

How do I run a command in docker terminal?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.


2 Answers

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

like image 145
gertas Avatar answered Oct 18 '22 00:10

gertas


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.

like image 43
Roland Weber Avatar answered Oct 18 '22 01:10

Roland Weber