Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #####################################################################################
- #
- # Title: dockerBuild.sh
- # Author: wildkindcc
- # Date: 06/09/2019
- # Description: Easy execute script for ongoing development with docker.
- #
- # Execution will kill any containers running for a defined image name,
- # re-build from a local Dockerfile and re-execute via "dockerRun" variable.
- #
- #####################################################################################
- # Configuration Options
- #
- #====================================================================================
- # Name is the image to build and create containers for. This can be anything, just make sure
- # it's unique, otherwise it will kill and remove other containers!
- name="pythonwebserver"
- # dockerRun is the final command to call which will re-load the container from the
- # freshly build image. Change this however it suits your application.
- dockerRun () {
- docker run -d -e PYTHONUNBUFFERED=0 -p 8000:8080 --rm $name
- }
- #====================================================================================
- #
- #####################################################################################
- # Coloured Output
- BLUE='\033[0;34m' # Blue
- GREEN='\033[0;32m' # Green
- RED='\033[0;31m' # Red
- NC='\033[0m' # No Color
- SUCCESS="${GREEN}[+] ${NC}"
- ERROR="${RED}[!] ${NC}"
- FAIL="${RED}[-] ${NC}"
- EVENT="${BLUE}[*] ${NC}"
- NOTIFICATION="[-] "
- err() {
- echo -e "${ERROR}An error occured, check output. Exiting..."
- exit 1
- }
- # Check if any containers for the defined image exist
- var=$(docker ps -a | grep $name | awk '{ print $1 }')
- if [[ -z $var ]]; then
- echo -e "${SUCCESS}No containers identified for image [$name]"
- else
- echo -e "${EVENT}The following containers are present for [$name]:"
- for i in $var; do
- echo -e "${EVENT}$i"
- done
- echo ""
- fi
- var=$(docker ps -a | grep $name | awk '{ print $1 }')
- # If containers exist, stop them
- if [[ ! -z $var ]]; then
- for i in $var; do
- docker stop $i > /dev/null \
- && echo -e "${SUCCESS}[${i}] stopped." || err
- done
- fi
- var=$(docker ps -a | grep $name | awk '{ print $1 }')
- # If containers exist, remove them (if they dont automatically)
- if [[ ! -z $var ]]; then
- for i in $var; do
- docker rm $i > /dev/null \
- && echo -e "${SUCCESS}[${i}] removed." || err
- done
- fi
- # Check if any images exit:
- var=$(docker images | grep $name | awk '{ print $3 }')
- if [[ -z $var ]]; then
- echo -e "${SUCCESS}No docker images identified for specified name [$name]"
- else
- echo -e "${EVENT}The following images are present for [$name]:"
- for i in $var; do
- echo -e "${EVENT}$i"
- done
- echo ""
- fi
- # If images exist, remove them
- if [[ ! -z $var ]]; then
- for i in $var; do
- docker rmi $i > /dev/null \
- && echo -e "${SUCCESS}[${i}] stopped." || err
- done
- fi
- echo -e "${SUCCESS}Housekeeping complete :o)"
- echo ""
- echo -e "${EVENT}Commencing build for [${name}]"
- docker build -t $name . \
- && echo -e "${SUCCESS}[${name}] built!." || err
- echo ""
- echo -e "${EVENT}See below for output of container [$name]"
- echo -e "${EVENT}---------------------------------------------------------------"
- dockerRun
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement