How to fix ctrl+c inside a docker container

43,660

Solution 1

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.

Starting with docker 0.6.5, you can add -t to the docker run command, which will attach a pseudo-TTY. Then you can type Control-C to detach from the container without terminating it.

If you use -t and -i then Control-C will terminate the container. When using -i with -t then you have to use Control-P Control-Q to detach without terminating.

Test 1:

$ ID=$(sudo docker run -t -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-P Control-Q
$ sudo docker ps

The container is still listed.

Test 2:

$ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps

the container is not there (it has been terminated). If you type Control-P Control-Q instead of Control-C in the 2nd example, the container would still be running.

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 might help: https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash

#!/bin/bash

# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
        echo "** Trapped CTRL-C"
}

for i in `seq 1 5`; do
    sleep 1
    echo -n "."
done

Solution 2

Use Ctrl+\ instead of Ctrl+C
it kills the process instead of politely asking it to shut down.(read more here.)

Solution 3

In some cases, when I used ctrl-C to terminate a process inside a container, the container terminates.

Additionally, I've seen cases where processes running inside containers leave zombie processes.

I have found that when starting a container with the "--init" switch, both of these problems are addressed. This appears to make my containers operate in a more "normal, expected UNIX-like manner".

Note: --init is different from -i, which is short for --interactive

If you want more information on what the "--init" switch does, please read up on it on the Docker web pages that include information on "docker run". The information on that web page says "Run an init inside the container that forwards signals and reaps processes".

Solution 4

I had the similar problem when I was trying to run mdbook (the Rust executable) in the docker container. The mdbook starts simple webserver and I want to stop it via Ctrl+C which did not work.

$ docker -ti --rm -p 4321:4321 my-docker-image mdbook serve --hostname 0.0.0.0 --port 4321
2019-08-16 14:00:11 [INFO] (mdbook::book): Book building has started
2019-08-16 14:00:11 [INFO] (mdbook::book): Running the html backend
2019-08-16 14:00:11 [INFO] (mdbook::cmd::serve): Serving on: http://0.0.0.0:4321
2019-08-16 14:00:11 [INFO] (ws): Listening for new connections on 0.0.0.0:3001.
2019-08-16 14:00:11 [INFO] (mdbook::cmd::watch): Listening for changes...
^C^C

Be inspired by @NID's answer I encapsulated the mdbook executable by universal bash script docker-entrypoint.sh which did the trick (without the need to explicitly catch the INT signal).

$ docker -ti --rm -p 4321:4321 my-docker-image docker-entrypoint.sh mdbook serve --hostname 0.0.0.0 --port 4321
2019-08-16 14:00:11 [INFO] (mdbook::book): Book building has started
2019-08-16 14:00:11 [INFO] (mdbook::book): Running the html backend
2019-08-16 14:00:11 [INFO] (mdbook::cmd::serve): Serving on: http://0.0.0.0:4321
2019-08-16 14:00:11 [INFO] (ws): Listening for new connections on 0.0.0.0:3001.
2019-08-16 14:00:11 [INFO] (mdbook::cmd::watch): Listening for changes...
^C $

The content of the docker-entrypoint.sh is very simple:

#!/bin/bash

$@

Solution 5

If you use Docker Compose, you can add the init parameter to forward signals to the container:

version: "2.4"
services:
  web:
    image: alpine:latest
    init: true

To make it work you need to have the option -ti in your docker exec command.

Share:
43,660

Related videos on Youtube

Jeanluca Scaljeri
Author by

Jeanluca Scaljeri

Updated on July 09, 2022

Comments

  • Jeanluca Scaljeri
    Jeanluca Scaljeri almost 2 years

    If I connect to a docker container

    $> docker exec -it my_container zsh
    

    and inside it I want to kill something I started with ctrl+c I noticed that it takes forever to complete. I've googled around and it seems that ctrl+c works a bit different than you would expect. My question, how can I fix ctrl+c inside a container ?

  • Jeanluca Scaljeri
    Jeanluca Scaljeri over 7 years
    I get the impression (and please correct me if I'm wrong) that what you describe ctrl+c is for exiting the container, which is not what I want. I want to stop a program I'v manually started
  • Greg Bell
    Greg Bell over 7 years
    Please elaborate on how a script would block the container process, and what is supposed to be done in the Ctrl-C trap.
  • eNca
    eNca over 4 years
    It does not work for me. When the running docker container does not respond to Ctrl+C then it ignore Ctrl+\ too
  • Jeanluca Scaljeri
    Jeanluca Scaljeri over 4 years
    This is nice, thnx. The only downside I see with this is that you have to write down the whole command
  • eNca
    eNca about 4 years
    Open your favorite editor, write there the two lines mentioned above and save it as docker-entrypoint.sh :-) Then add this file to the image via ADD command in your Dockerfile.
  • ccj5351
    ccj5351 almost 4 years
    I am using Ubuntu 16.04.
  • ccj5351
    ccj5351 almost 4 years
    Or you can also 1) run "docker exec -it SOME_CONTAINER_ID ps -aus" to find the SOME_PID in the container; then 2) run "docker exec -it SOME_CONTAINER_ID kill -9 SOME_PID" to kill the process inside the docker container.
  • Maciek Rek
    Maciek Rek over 3 years
    OT: I think stopping the container with CTRL+C is a bad design...I keep accidentally stopping the container simply because of a habit of exiting shell process this way.
  • Nigrimmist
    Nigrimmist over 3 years
    Ctrl+Z works fine after docker run, thanks!
  • RayLuo
    RayLuo about 3 years
    Thanks! Now I really know why it would be a good idea to always add -it. By the way, it is easy to remember too. Just docker run -it ...! :-D