docker-entrypoint.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/sh
  2. shutdown() {
  3. echo "shutting down container"
  4. # first shutdown any service started by runit
  5. for _srv in $(ls -1 /etc/service); do
  6. sv force-stop $_srv
  7. done
  8. # shutdown runsvdir command
  9. kill -HUP $RUNSVDIR
  10. wait $RUNSVDIR
  11. # give processes time to stop
  12. sleep 0.5
  13. # kill any other processes still running in the container
  14. for _pid in $(ps -eo pid | grep -v PID | tr -d ' ' | grep -v '^1$' | head -n -6); do
  15. timeout -t 5 /bin/sh -c "kill $_pid && wait $_pid || kill -9 $_pid"
  16. done
  17. exit
  18. }
  19. echo "Starting startup scripts in /docker-entrypoint-init.d ..."
  20. for script in $(find /docker-entrypoint-init.d/ -executable -type f); do
  21. echo >&2 "*** Running: $script"
  22. $script
  23. retval=$?
  24. if [ $retval != 0 ];
  25. then
  26. echo >&2 "*** Failed with return value: $?"
  27. exit $retval
  28. fi
  29. done
  30. echo "Finished startup scripts in /docker-entrypoint-init.d"
  31. echo "Starting runit..."
  32. exec runsvdir -P /etc/service &
  33. RUNSVDIR=$!
  34. echo "Started runsvdir, PID is $RUNSVDIR"
  35. echo "wait for processes to start...."
  36. sleep 5
  37. for _srv in $(ls -1 /etc/service); do
  38. sv status $_srv
  39. done
  40. # catch shutdown signals
  41. trap shutdown SIGTERM SIGHUP SIGQUIT SIGINT
  42. wait $RUNSVDIR
  43. shutdown