run-docker.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 UI"
  61. pushd ui
  62. yarn build
  63. popd
  64. echo "Building GeoGuessr Self Host image as ggsh:latest"
  65. docker build -t ggsh:latest .
  66. echo "Executing new container $CONTAINER_NAME using ggsh:latest"
  67. docker run -p$PORT:80 --name $CONTAINER_NAME -d ggsh
  68. echo "Geoguessr Self Host UI accessible at http://localhost:$PORT/"
  69. echo "Geoguessr Self Host API accessible at http://localhost:$PORT/api/"
  70. ;;
  71. "c"|"clean")
  72. clean_container
  73. ;;
  74. "s"|"status")
  75. STATUS=$(status_check)
  76. if [ "$STATUS" = "true" ]
  77. then
  78. echo "Existing container $CONTAINER_NAME is running."
  79. elif [ "$STATUS" = "false" ]
  80. then
  81. echo "Existing container $CONTAINER_NAME is stopped."
  82. else
  83. echo "No existing container $CONTAINER_NAME could be found."
  84. fi
  85. ;;
  86. *)
  87. echo "Unknown option $1, must be one of help, status, clean, run, logs."
  88. exit 1
  89. ;;
  90. esac