run-docker.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env sh
  2. if [ $# -lt 1 ] || [ "$1" == "help" ] || [ "$1" == "h" ]
  3. then
  4. echo "usage: $0 (help|status|clean|run|logs) [container-name]"
  5. echo " help - print this message"
  6. echo " status - report whether a container named container-name can be found, and whether or not it is running"
  7. echo " clean - kill and remove any existing containers matching container-name"
  8. echo " run - kill and remove any existing containers matching container-name, build the current directory into a new image, and deploy a new container container-name"
  9. echo " logs - tail the logs of the container named container-name, equivalent to docker logs -f container-name"
  10. echo " container-name defaults to ggsh-instance"
  11. echo " Run as root to ensure docker can be used, or set FORCE_NOROOT=* to force the script to run anyway."
  12. exit 1
  13. fi
  14. if [ "$EUID" -ne 0 ] && [ -z "$FORCE_NOROOT" ]
  15. then
  16. echo "$0 must be run as root to use docker. Set FORCE_NOROOT=* to force the script to run anyway."
  17. exit 1
  18. fi
  19. if [ $# -gt 1 ]
  20. then
  21. CONTAINER_NAME=$2
  22. else
  23. CONTAINER_NAME="ggsh-instance"
  24. fi
  25. if [ -z "$PORT" ]
  26. then
  27. echo "Defaulting to PORT=8080"
  28. PORT=8080
  29. fi
  30. status_check() {
  31. docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2> /dev/null
  32. }
  33. clean_container() {
  34. STATUS=$(status_check)
  35. if [ "$STATUS" = "true" ]
  36. then
  37. echo "Container $CONTAINER_NAME is running and will be stopped and removed."
  38. docker kill $CONTAINER_NAME
  39. docker rm $CONTAINER_NAME
  40. elif [ "$STATUS" = "false" ]
  41. then
  42. echo "Container $CONTAINER_NAME is stopped and will be removed."
  43. docker rm $CONTAINER_NAME
  44. else
  45. echo "No existing container $CONTAINER_NAME could be found, no cleanup will be performed."
  46. fi
  47. }
  48. case $1 in
  49. "l"|"logs")
  50. STATUS=$(status_check)
  51. if [ "$STATUS" = "true" ] || [ "$STATUS" = "false" ]
  52. then
  53. docker logs -f $CONTAINER_NAME
  54. else
  55. echo "No existing container $CONTAINER_NAME could be found."
  56. fi
  57. ;;
  58. "r"|"run")
  59. clean_container
  60. echo "Building GeoGuessr Self Host image as ggsh:latest"
  61. docker build -t ggsh:latest .
  62. echo "Executing new container $CONTAINER_NAME using ggsh:latest"
  63. docker run -p$PORT:80 --name $CONTAINER_NAME -d ggsh
  64. echo "Geoguessr Self Host UI accessible at http://localhost:$PORT/"
  65. echo "Geoguessr Self Host API accessible at http://localhost:$PORT/api/"
  66. ;;
  67. "c"|"clean")
  68. clean_container
  69. ;;
  70. "s"|"status")
  71. STATUS=$(status_check)
  72. if [ "$STATUS" = "true" ]
  73. then
  74. echo "Existing container $CONTAINER_NAME is running."
  75. elif [ "$STATUS" = "false" ]
  76. then
  77. echo "Existing container $CONTAINER_NAME is stopped."
  78. else
  79. echo "No existing container $CONTAINER_NAME could be found."
  80. fi
  81. ;;
  82. *)
  83. echo "Unknown option $1, must be one of help, status, clean, run, logs."
  84. exit 1
  85. ;;
  86. esac