Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make container exit gracefully and with correct exit code
While fiddling around with multiple child processes, and giving them some nice "exit 1" error codes, I noticed that the container would always exit with exit code "0". It was not until I placed an "exit 2" above the 'trap "kill 0" EXIT' row that I started getting the expected exit codes again. I dived a bit deeper into this, and realized that this thing with properly handling kill signals is pretty tedious. Nevertheless, after reading a lot of articles (linked at the bottom) I finally managed to create something which seems to work as intended. The first trap will catch the SIGINT (Ctrl+C) and the SIGTERM (docker stop ...) singnals, and make so that both of these trigger a normal "exit" command in the script. The second trap is special for bash (the 'EXIT' at the end of the trap command), since this allows you to trigger a "cleanup script" before exiting the main program. I made so that it will run the function "clean_exit" before terminating with the correct exit code. The "clean_exit" script just sends a SIGTERM signal to the PIDs of our two child processes (if they exists and have started), which gives them a chance to exit gacefully. By implementing this we can now exit the container in a controlled and graceful manner, while also having it report the correct exit code depending on what happened. [1]: http://veithen.io/2014/11/16/sigterm-propagation.html [2]: https://www.linuxjournal.com/content/bash-trap-command [3]: https://blog.codeship.com/trapping-signals-in-docker-containers/ [4]: https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/ [5]: https://unix.stackexchange.com/a/444676 [6]: https://blog.phusion.nl/2015/01/20/docker-and-the-pid-1-zombie-reaping-problem/ [7]: https://stackoverflow.com/a/35410993 [8]: https://unix.stackexchange.com/q/317492
- Loading branch information
43dde6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An extra note to this is that
"kill 0"
is not a totally wrong thing to call on as the "cleanup script" before exiting. The0
here is a special case which signals all processes in the current process group, so that would mean us and all our children [1], [2].However, after running that comand it seems like the new exit code will almost always become
0
, since we will most certainly be able to kill theentrypoint.sh
bash script without any problems. The bad thing about this is that it masks all the other error codes which may arise.Using this new method, which is more verbose and less generic, is that it is possible to get an exit status like
130
( =128+2
-> Container terminated byCtrl+C
). This will greatly help if you are trying to do different things with the Docker containers depending on how they exit, or if you are tying to troubleshoot.