I have the following two files in a directory:
Dockerfile
FROM debian
WORKDIR /app
COPY start.sh /app/
CMD ["/app/start.sh"]
start.sh
(with permissions 755 using chmod +x start.sh
)
#!/bin/bash
trap "echo SIGINT; exit" SIGINT
trap "echo SIGTERM; exit" SIGTERM
echo Starting script
sleep 100000
I then run the following commands:
$ docker build . -t tmp
$ docker run --name tmp tmp
I then expect that pressing Ctrl+C would send a SIGINT to the program, which would print SIGINT to the screen then exit, but that doesn’t happen.
I also try running $ docker stop tmp
, which I expect would send a SIGTERM to the program, but checking $ docker logs tmp
after shows that SIGTERM was not caught.
Why are SIGINT and SIGTERM not being caught by the bash script?
3
Answers
CTRL+C sends a signal to
docker
running on that console.To send a signal to the script you could use
Or include
echo "my PID: $$"
on your script and sendSome shell implementations in docker might ignore the signal.
This script will correctly react to
pkill -15
. Please note that signals are specified without theSIG
prefix.The long
sleep
command was replaced by an infinite loop of short ones sincesleep
may ignore some signals.Actually, your
Dockerfile
andstart.sh
entrypoint script work as is for me with Ctrl+C, provided you run the container with one of the following commands:docker run --name tmp -it tmp
docker run --rm -it tmp
Documentation details
As specified in
docker run --help
:--interactive
=-i
CLI flag asks to keep STDIN open even if not attached(typically useful for an interactive shell, or when also passing the
--detach
=-d
CLI flag)--tty
=-t
CLI flag asks to allocate a pseudo-TTY(which notably forwards signals to the shell entrypoint, especially useful for your use case)
Related remarks
For completeness, note that there are several related issues that can make
docker stop
take too much time and "fall back" todocker kill
, which can arise when the shell entrypoint starts some other process(es):exec
builtin:exec prog arg1 arg2 ...
INT
/TERM
, but notKILL
) is very important;{see also this SO question: Docker Run Script to catch interruption signal}
{see also this SO question for details: Speed up docker-compose shutdown}
The solution I found was to just use the
--init
flag.Per their docs…