Ctrl+C does nothing when I run a process in a Docker container
Ctrl+C prints ^C but the container keeps runningShort answer
Ctrl+C sends SIGINT, and the main process of a container runs as PID 1, which Linux treats specially: it ignores signals unless the program handles them itself. Run with -it so the terminal is attached, add --init so a small init process forwards signals, and do not turn off --sig-proxy.
Fix it fast
1Find out which cause you have
Check how the container was started. The three things that matter are whether it has a terminal, whether it has an init process, and whether signals are proxied.
docker ps docker inspect --format='{{json .Config}}' my-app2Match what you see, then jump to its fix
- 1
docker run without -i and -t, so there is no terminal for Ctrl+C to act through.The container was started without an interactive terminalFix - 2
The program runs fine with -it, but Ctrl+C is ignored until you kill the container from another terminal.The main process is PID 1 and ignores SIGINTFix - 3
Your run command or an alias contains --sig-proxy=false.Signal proxying was turned off with --sig-proxy=falseFix
- 1
What does the error look like?
Ctrl+C prints ^C but the container keeps running
Cause 1 of 3
The container was started without an interactive terminal
1Check
-i keeps STDIN open and -t allocates a pseudo-TTY. They are normally used together for an interactive session.
docker run --rm my-image2Fix
Start the container with -it.
docker run --rm -it my-image3Verify
Press Ctrl+C in the new session and the foreground process stops.
docker psConfirmed by docs.docker.com
Cause 2 of 3
The main process is PID 1 and ignores SIGINT
1Check
Look at the run command for --init. Without it, a process running as PID 1 in a container ignores signals with the default action, so it does not stop on SIGINT or SIGTERM unless the program has its own handler.
history | grep "docker run"2Fix
Run with --init, which puts an init process at PID 1 that forwards signals and reaps child processes. Adding a signal handler in the application also works.
docker run --rm -it --init my-image3Verify
Ctrl+C now ends the process and the container exits.
docker ps -aConfirmed by docs.docker.com
Cause 3 of 3
Signal proxying was turned off with --sig-proxy=false
1Check
--sig-proxy is on by default and proxies received signals to the process. When it is false, the client does not pass them on.
alias | grep docker; history | grep sig-proxy2Fix
Remove the flag from the command or the alias.
docker run --rm -it --init my-image3Verify
Ctrl+C reaches the process.
docker psConfirmed by docs.docker.com
How do I stop it coming back?
- Use --init for any image whose main process is a plain script or server that has no signal handling.
- Use -it for anything you run in the foreground, and -d for background services.
- To leave an attached session without stopping the container, use the detach keys instead of Ctrl+C.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
docker stop hangs, or the container will not stop or stays down
docker kill sends a signal and the container keeps running
docker stop sends SIGTERM to the main process and waits 10 seconds before SIGKILL.
4 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fix