Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Author: Benjamin Weidig <ben@netzgut.net>
  4. # Version: 1 (2017-02-19)
  5.  
  6. # A helper shell script for starting a Docker container in a more flexible way.
  7. # A container might not start if all the linked containers or ports are not
  8. # available, so now we are able to specify optional resources.
  9. # This script is used at Netzgut to start desktop applications with Docker.
  10.  
  11. # REQUIRED: The image you want to use
  12. IMAGE="desktop/eclipse"
  13.  
  14. # REQUIRED: The name of the container, needed for identification
  15. CONTAINER_NAME=eclipse
  16.  
  17. # OPTIONAL: Connect to specific docker network
  18. NETWORK="docker_dev"
  19.  
  20. # Removes the container if it's already running.
  21. # This might be a problem for you because you could destroy unsaved progress.
  22. # If "0" then the container will only be removed if not already running and no
  23. # new container will be started.
  24. KILL_IF_RUNNING=1
  25.  
  26. # OPTIONAL: Additional arguments that will just be passed directly to docker
  27. ARGS=()
  28.  
  29. # OPTIONAL: This volumes will be created if they don't exist.
  30. # Why? Because if the docker-engine would create them they would
  31. # be owned by root. That's not necessary a problem but the files
  32. # will be easier to use outside of the container this way.
  33. # CAVEAT: No "~"-expansion is supported, so use $HOME
  34. VOLUMES_ENSURE=(
  35. $HOME/code/eclipse/:/home/docker/workspaces
  36. $HOME/code:/home/docker/code
  37. $HOME/.docker-data/eclipse:/home/docker/.eclipse
  38. $HOME/.m2/repository:/home/docker/.m2/repository
  39. )
  40.  
  41. # OPTIONAL: This volumes will be mounted "normally" without any checks from us.
  42. # CAVEAT: No "~"-expansion is supported, so use $HOME
  43. VOLUMES=(
  44. $HOME/.dotfiles/gitconfig:/home/docker/.gitconfig:ro
  45. $HOME/.dotfiles/hgrc:/home/docker/.hgrc:ro
  46. $HOME/.hgext/hgflow.py:/home/docker/.hgext/hgflow.py:ro
  47. $HOME/.hgext/mercurial_keyring.py:/home/docker/.hgext/mercurial_keyring.py:ro
  48. $HOME/.dotfiles/m2/settings.xml:/home/docker/.m2/settings.xml:ro
  49. /etc/localtime:/etc/localtime:ro
  50. /etc/timezone:/etc/timezone:ro
  51. /tmp/.X11-unix:/tmp/.X11-unix
  52. )
  53.  
  54. # OPTIONAL: Links to other containers that are not checked if they are actually running.
  55. # Normally that's a problem, except when you run in a network that actually defines the
  56. # container.
  57. LINKS=(
  58. mongo:mongo
  59. mysql:mysql
  60. )
  61.  
  62. # OPTIONAL: These links are optional and won't be linked if not available.
  63. LINKS_OPTIONAL=(
  64. postgres:postgres
  65. rabbitmq:rabbitmq
  66. )
  67.  
  68. # OPTIONAL: Required ports. We won't do any checks, let the docker-engine do its work.
  69. PORTS=(
  70. 8080:8080
  71. 8081:8081
  72. )
  73.  
  74. # OPTIONAL: These ports are optional. We check with netstat if they are available.
  75. PORTS_OPTIONAL=(
  76. 8443:8443
  77. 8888:8888
  78. )
  79.  
  80. # OPTIONAL: Sets environment variables.
  81. ENVS=(
  82. "DISPLAY=$DISPLAY"
  83. SWT_GTK3=0
  84. MEM_XMS=512m
  85. MEM_XMX=3G
  86. MEM_MAX_PERM_SIZE=3G
  87. )
  88.  
  89. ###############################################################################
  90. ###############################################################################
  91. #### DO NOT CHANGE BELOW THIS BLOCK ####### DO NOT CHANGE BELOW THIS BLOCK ####
  92. ###############################################################################
  93. ###############################################################################
  94.  
  95. # Notification system: The MESSAGES will be displayed to
  96. # e.g. inform the user about the actual start parameters
  97. # if not all optionals could be fulfilled.
  98. MESSAGES=()
  99. function notify {
  100. if [ ! -z "$MESSAGES" ]; then
  101. notify-send "$1" "${MESSAGES[@]}"
  102. fi
  103. }
  104.  
  105.  
  106. ###############################################################################
  107. # Step 1: Remove a previously running container by its name. #
  108. ###############################################################################
  109. if [ $KILL_IF_RUNNING -eq 1 ]; then
  110. echo "KILLING"
  111. docker rm -f $CONTAINER_NAME 2> /dev/null
  112. else
  113. echo "NO KILLING"
  114. docker top $CONTAINER_NAME &>/dev/null
  115. if [ $? -eq 0 ]; then
  116. MESSAGES+="Container is already running and KILL_IF_RUNNING=0"
  117. notify "Couldn't start container $CONTAINER_NAME"
  118. exit -1
  119. fi
  120. fi
  121.  
  122. ###############################################################################
  123. # Step 2: VOLUMES #
  124. ###############################################################################
  125. ALL_VOLUMES=()
  126.  
  127. # Check the volumes that might need creation.
  128. if [ ! -z "$VOLUMES_ENSURE" ]; then
  129. VOLUMES_MESSAGE=0
  130. for VOLUME in ${VOLUMES_ENSURE[@]}
  131. do
  132. HOST_VOLUME=${VOLUME%:*}
  133. if [ ! -d $HOST_VOLUME ]; then
  134. mkdir -p $HOST_VOLUME 2> /dev/null
  135. if [ VOLUMES_MESSAGE -eq 0 ]; then
  136. MESSAGES+="\nCreated Volumes:"
  137. VOLUMES_MESSAGE=1
  138. fi
  139. MESSAGES+="\n- $HOST_VOLUME"
  140. fi
  141. ALL_VOLUMES+=($VOLUME)
  142. done
  143. fi
  144.  
  145. # If we got any other volumes add them to the all volumes, too.
  146. if [ ! -z "$VOLUMES" ]; then
  147. ALL_VOLUMES=("${ALL_VOLUMES[@]}" "${VOLUMES[@]}")
  148. fi
  149.  
  150.  
  151. ###############################################################################
  152. # Step 3: LINKS #
  153. ###############################################################################
  154. ALL_LINKS=()
  155.  
  156. # Check if the optional links are running.
  157. if [ ! -z "$LINKS_OPTIONAL" ]; then
  158. LINKS_MESSAGE=0
  159. for LINK in ${LINKS_OPTIONAL[@]}; do
  160. HOST_LINK=${LINK%:*}
  161. docker top $HOST_LINK &>/dev/null
  162. if [ $? -eq 0 ]; then
  163. ALL_LINKS+=($LINK)
  164. else
  165. if [[ $LINKS_MESSAGE -eq 0 ]]; then
  166. MESSAGES+="\nUnavailable linked containers:"
  167. LINKS_MESSAGE=1
  168. fi
  169. MESSAGES+="\n- $HOST_LINK"
  170. fi
  171. done
  172. fi
  173.  
  174. # Add required links to all links.
  175. if [ ! -z "$LINKS" ]; then
  176. ALL_LINKS=("${ALL_LINKS[@]}" "${LINKS[@]}")
  177. fi
  178.  
  179.  
  180. ###############################################################################
  181. # Step 4: PORTS #
  182. ###############################################################################
  183. ALL_PORTS=()
  184.  
  185. # Check if the optional ports are available.
  186. if [ ! -z "$PORTS_OPTIONAL" ]; then
  187. PORTS_MESSAGE=()
  188. for PORT in ${PORTS_OPTIONAL[@]}; do
  189. HOST_PORT=${PORT%:*}
  190. OUT=$(netstat -lnt | awk -v port=$HOST_PORT '$6 == "LISTEN" && $4 ~ port')
  191. if [[ -z $OUT ]]
  192. then
  193. ALL_PORTS+=($PORT)
  194. else
  195. if [[ $PORTS_MESSAGE -eq 0 ]]; then
  196. MESSAGES+="\nUnavailable port:"
  197. PORTS_MESSAGE=1
  198. fi
  199. MESSAGES+="\n- $HOST_PORT"
  200. fi
  201. done
  202. fi
  203.  
  204. # Add any required ports to the list.
  205. if [ ! -z "$PORTS" ]; then
  206. ALL_PORTS=("${ALL_PORTS[@]}" "${PORTS[@]}")
  207. fi
  208.  
  209. ###############################################################################
  210. # STEP 5: Build the actual "docker run" arguments #
  211. ###############################################################################
  212. ALL_ARGS=(
  213. "--rm"
  214. "--name $CONTAINER_NAME")
  215.  
  216. # Add any passthrough arguments
  217. if [ ! -z "$ARGS" ]; then
  218. ALL_ARGS=("${ALL_ARGS[@]}" "${ARGS[@]}")
  219. fi
  220.  
  221. # Add network if set
  222. if [ ! -z "$NETWORK" ]; then
  223. ALL_ARGS+=("--net $NETWORK")
  224. fi
  225.  
  226. # Add all volumes
  227. for VOLUME in ${ALL_VOLUMES[@]}; do
  228. ALL_ARGS+=("-v $VOLUME")
  229. done
  230.  
  231. # Add all links
  232. for LINK in ${ALL_LINKS[@]}; do
  233. ALL_ARGS+=("--link $LINK")
  234. done
  235.  
  236. # Add all ports
  237. for PORT in ${ALL_PORTS[@]}; do
  238. ALL_ARGS+=("-p $PORT")
  239. done
  240.  
  241.  
  242. # Add environment variables
  243. if [ ! -z "$ENVS" ]; then
  244. for ENV in ${ENVS[@]}; do
  245. ALL_ARGS+=("-e $ENV")
  246. done
  247. fi
  248.  
  249. # Add image name last
  250. ALL_ARGS+=($IMAGE)
  251.  
  252. ###############################################################################
  253. # Step 6: Notify user and start container
  254. ###############################################################################
  255. notify "Started Container $CONTAINER_NAME"
  256.  
  257. docker run ${ALL_ARGS[@]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement