Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Ctrl+C does nothing when I run a process in a Docker container

ErrorCtrl+C prints ^C but the container keeps running

Short 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.

  • Applies to Docker Engine 28, Docker Desktop 4.x, Linux containers
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-app
  2. 2Match what you see, then jump to its fix

    1. 1docker 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. 2The 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. 3Your run command or an alias contains --sig-proxy=false.Signal proxying was turned off with --sig-proxy=falseFix

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-image

2Fix

Start the container with -it.

docker run --rm -it my-image

3Verify

Press Ctrl+C in the new session and the foreground process stops.

docker ps

Confirmed by docs.docker.com

Did this fix it?

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-image

3Verify

Ctrl+C now ends the process and the container exits.

docker ps -a

Confirmed by docs.docker.com

Did this fix it?

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-proxy

2Fix

Remove the flag from the command or the alias.

docker run --rm -it --init my-image

3Verify

Ctrl+C reaches the process.

docker ps

Confirmed by docs.docker.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.