run-docker.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. status_check() {
  26. docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2> /dev/null
  27. }
  28. clean_container() {
  29. STATUS=$(status_check)
  30. if [ "$STATUS" = "true" ]
  31. then
  32. echo "Container $CONTAINER_NAME is running and will be stopped and removed."
  33. docker kill $CONTAINER_NAME
  34. docker rm $CONTAINER_NAME
  35. elif [ "$STATUS" = "false" ]
  36. then
  37. echo "Container $CONTAINER_NAME is stopped and will be removed."
  38. docker rm $CONTAINER_NAME
  39. else
  40. echo "No existing container $CONTAINER_NAME could be found, no cleanup will be performed."
  41. fi
  42. }
  43. case $1 in
  44. "l"|"logs")
  45. STATUS=$(status_check)
  46. if [ "$STATUS" = "true" ] || [ "$STATUS" = "false" ]
  47. then
  48. docker logs -f $CONTAINER_NAME
  49. else
  50. echo "No existing container $CONTAINER_NAME could be found."
  51. fi
  52. ;;
  53. "r"|"run")
  54. clean_container
  55. echo "Building UI"
  56. pushd ui
  57. yarn build
  58. popd
  59. echo "Building GeoGuessr Self Host image as ggsh:latest"
  60. docker build -t ggsh:latest .
  61. echo "Executing new container $CONTAINER_NAME using ggsh:latest"
  62. docker run -p6070:80 --name $CONTAINER_NAME -d ggsh
  63. echo "Geoguessr Self Host UI accessible at http://localhost:6070/"
  64. echo "Geoguessr Self Host API accessible at http://localhost:6070/api/"
  65. ;;
  66. "c"|"clean")
  67. clean_container
  68. ;;
  69. "s"|"status")
  70. STATUS=$(status_check)
  71. if [ "$STATUS" = "true" ]
  72. then
  73. echo "Existing container $CONTAINER_NAME is running."
  74. elif [ "$STATUS" = "false" ]
  75. then
  76. echo "Existing container $CONTAINER_NAME is stopped."
  77. else
  78. echo "No existing container $CONTAINER_NAME could be found."
  79. fi
  80. ;;
  81. *)
  82. echo "Unknown option $1, must be one of help, status, clean, run, logs."
  83. exit 1
  84. ;;
  85. esac