I had to figure this out recently. After much frustration, what I landed on is this:
In your dockerfile, specify an ENTRYPOINT:
ENTRYPOINT ["/entrypoint.sh"]
Then, provide such a script. It doesn't need to do anything other than invoke your application. Feel free to add additional setup to the script, but be mindful that if the script does anything after it invokes your application, it could mask your application's return code. If that's relevant to you, make sure to have the script capture the return code and propagate it out as its own return code.
Note: Do not use exec
to invoke your application!
The result will be that PID 1 belongs to entrypoint.sh, and your application will have some other PID. Mine tend to land on PID 9 or 10.
Then, when you need to kill your application, simply determine its PID and invoke kill -SIGKILL $PID
, or pkill -SIGKILL yourapp
. Your process, not being PID 1, will receive the signal and exit immediately. Your entrypoint.sh
will promptly exit, because that's what you designed it to do after your process exits.