1
0

build-local-docker-image.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  3. LOG_FILE=${SCRIPT_DIR}/build-local-docker-image.log
  4. ERROR=""
  5. IMAGE_NAME="vben-admin-local"
  6. function stop_and_remove_container() {
  7. # Stop and remove the existing container
  8. docker stop ${IMAGE_NAME} >/dev/null 2>&1
  9. docker rm ${IMAGE_NAME} >/dev/null 2>&1
  10. }
  11. function remove_image() {
  12. # Remove the existing image
  13. docker rmi vben-admin-pro >/dev/null 2>&1
  14. }
  15. function install_dependencies() {
  16. # Install all dependencies
  17. cd ${SCRIPT_DIR}
  18. pnpm install || ERROR="install_dependencies failed"
  19. }
  20. function build_image() {
  21. # build docker
  22. docker build ../../ -f Dockerfile -t ${IMAGE_NAME} || ERROR="build_image failed"
  23. }
  24. function log_message() {
  25. if [[ ${ERROR} != "" ]];
  26. then
  27. >&2 echo "build failed, Please check build-local-docker-image.log for more details"
  28. >&2 echo "ERROR: ${ERROR}"
  29. exit 1
  30. else
  31. echo "docker image with tag '${IMAGE_NAME}' built sussessfully. Use below sample command to run the container"
  32. echo ""
  33. echo "docker run -d -p 8010:8080 --name ${IMAGE_NAME} ${IMAGE_NAME}"
  34. fi
  35. }
  36. echo "Info: Stopping and removing existing container and image" | tee ${LOG_FILE}
  37. stop_and_remove_container
  38. remove_image
  39. echo "Info: Installing dependencies" | tee -a ${LOG_FILE}
  40. install_dependencies 1>> ${LOG_FILE} 2>> ${LOG_FILE}
  41. if [[ ${ERROR} == "" ]]; then
  42. echo "Info: Building docker image" | tee -a ${LOG_FILE}
  43. build_image 1>> ${LOG_FILE} 2>> ${LOG_FILE}
  44. fi
  45. log_message | tee -a ${LOG_FILE}