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?
Actually, your Dockerfile
and start.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
As specified in docker run --help
:
--interactive
= -i
CLI flag asks to keep STDIN open even if not attached--detach
= -d
CLI flag)--tty
= -t
CLI flag asks to allocate a pseudo-TTYFor completeness, note that there are several related issues that can make docker stop
take too much time and "fall back" to docker kill
, which can arise when the shell entrypoint starts some other process(es):
exec
builtin:exec prog arg1 arg2 ...
INT
/ TERM
, but not KILL
) is very important;If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With