Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #!/bin/bash
  2. #####################################################################################
  3. #
  4. # Title: dockerBuild.sh
  5. # Author: wildkindcc
  6. # Date: 06/09/2019
  7. # Description: Easy execute script for ongoing development with docker.
  8. #
  9. # Execution will kill any containers running for a defined image name,
  10. # re-build from a local Dockerfile and re-execute via "dockerRun" variable.
  11. #
  12. #####################################################################################
  13. # Configuration Options
  14. #
  15. #====================================================================================
  16. # Name is the image to build and create containers for. This can be anything, just make sure
  17. # it's unique, otherwise it will kill and remove other containers!
  18. name="pythonwebserver"
  19.  
  20. # dockerRun is the final command to call which will re-load the container from the
  21. # freshly build image. Change this however it suits your application.
  22. dockerRun () {
  23. docker run -d -e PYTHONUNBUFFERED=0 -p 8000:8080 --rm $name
  24. }
  25.  
  26. #====================================================================================
  27. #
  28. #####################################################################################
  29. # Coloured Output
  30. BLUE='\033[0;34m' # Blue
  31. GREEN='\033[0;32m' # Green
  32. RED='\033[0;31m' # Red
  33. NC='\033[0m' # No Color
  34.  
  35. SUCCESS="${GREEN}[+] ${NC}"
  36. ERROR="${RED}[!] ${NC}"
  37. FAIL="${RED}[-] ${NC}"
  38. EVENT="${BLUE}[*] ${NC}"
  39. NOTIFICATION="[-] "
  40.  
  41. err() {
  42. echo -e "${ERROR}An error occured, check output. Exiting..."
  43. exit 1
  44. }
  45.  
  46. # Check if any containers for the defined image exist
  47. var=$(docker ps -a | grep $name | awk '{ print $1 }')
  48. if [[ -z $var ]]; then
  49. echo -e "${SUCCESS}No containers identified for image [$name]"
  50. else
  51. echo -e "${EVENT}The following containers are present for [$name]:"
  52. for i in $var; do
  53. echo -e "${EVENT}$i"
  54. done
  55. echo ""
  56. fi
  57. var=$(docker ps -a | grep $name | awk '{ print $1 }')
  58. # If containers exist, stop them
  59. if [[ ! -z $var ]]; then
  60. for i in $var; do
  61. docker stop $i > /dev/null \
  62. && echo -e "${SUCCESS}[${i}] stopped." || err
  63. done
  64. fi
  65. var=$(docker ps -a | grep $name | awk '{ print $1 }')
  66. # If containers exist, remove them (if they dont automatically)
  67. if [[ ! -z $var ]]; then
  68. for i in $var; do
  69. docker rm $i > /dev/null \
  70. && echo -e "${SUCCESS}[${i}] removed." || err
  71. done
  72. fi
  73.  
  74. # Check if any images exit:
  75. var=$(docker images | grep $name | awk '{ print $3 }')
  76. if [[ -z $var ]]; then
  77. echo -e "${SUCCESS}No docker images identified for specified name [$name]"
  78. else
  79. echo -e "${EVENT}The following images are present for [$name]:"
  80. for i in $var; do
  81. echo -e "${EVENT}$i"
  82. done
  83. echo ""
  84. fi
  85. # If images exist, remove them
  86. if [[ ! -z $var ]]; then
  87. for i in $var; do
  88. docker rmi $i > /dev/null \
  89. && echo -e "${SUCCESS}[${i}] stopped." || err
  90. done
  91. fi
  92.  
  93. echo -e "${SUCCESS}Housekeeping complete :o)"
  94. echo ""
  95. echo -e "${EVENT}Commencing build for [${name}]"
  96. docker build -t $name . \
  97. && echo -e "${SUCCESS}[${name}] built!." || err
  98. echo ""
  99. echo -e "${EVENT}See below for output of container [$name]"
  100. echo -e "${EVENT}---------------------------------------------------------------"
  101.  
  102. dockerRun
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement