From 524b9f1c0e48fdbf5251bdcaed8fee935876bf23 Mon Sep 17 00:00:00 2001 From: Arnie Date: Thu, 4 Jan 2018 06:34:06 +0100 Subject: [PATCH] Initial commit --- adminer/Dockerfile | 5 ++ adminer/run.sh | 53 ++++++++++++++++ common.shinc | 104 ++++++++++++++++++++++++++++++ functions.shinc | 108 ++++++++++++++++++++++++++++++++ mailhog/run.sh | 48 ++++++++++++++ maria-db/run.sh | 69 ++++++++++++++++++++ nginx/Dockerfile | 5 ++ nginx/conf.d/proxy_timeout.conf | 4 ++ nginx/conf.d/request_size.conf | 1 + nginx/run.sh | 58 +++++++++++++++++ pgsql-db/run.sh | 46 ++++++++++++++ settings.shinc | 41 ++++++++++++ 12 files changed, 542 insertions(+) create mode 100644 adminer/Dockerfile create mode 100755 adminer/run.sh create mode 100644 common.shinc create mode 100644 functions.shinc create mode 100755 mailhog/run.sh create mode 100755 maria-db/run.sh create mode 100644 nginx/Dockerfile create mode 100644 nginx/conf.d/proxy_timeout.conf create mode 100644 nginx/conf.d/request_size.conf create mode 100755 nginx/run.sh create mode 100755 pgsql-db/run.sh create mode 100644 settings.shinc diff --git a/adminer/Dockerfile b/adminer/Dockerfile new file mode 100644 index 0000000..ced73f8 --- /dev/null +++ b/adminer/Dockerfile @@ -0,0 +1,5 @@ +ARG IMAGE_NAME +ARG IMAGE_VERSION +FROM ${IMAGE_NAME}:${IMAGE_VERSION} + +RUN sed -i 's/max_execution_time.*/max_execution_time = 600/g' /etc/php5/cli/php.ini && sed -i 's/max_input_time.*/max_input_time = 600/g' /etc/php5/fpm/php.ini diff --git a/adminer/run.sh b/adminer/run.sh new file mode 100755 index 0000000..6a8387b --- /dev/null +++ b/adminer/run.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P ) + +IMAGE_NAME="clue/adminer" +IMAGE_VERSION="latest" + +PROJECT_NAME=adminer + + +source ${SCRIPT_PATH}/../common.shinc + +# +# Project specific variables +# + +DOMAIN_NAME="adminer.loc" + +init() { + __init + + # Create networks + NETWORKS=(${DB_NETWORK} ${PROXY_NETWORK}) + __createNetworks + + __build || return $? + +echo " docker create \ + --name ${PROJECT_NAME} \ + -e VIRTUAL_HOST=${DOMAIN_NAME} \ + --restart=unless-stopped \ + --net ${DB_NETWORK} \ + --net ${PROXY_NETWORK} \ + ${PROJECT_NAME}:latest" + + docker create \ + --name ${PROJECT_NAME} \ + -e VIRTUAL_HOST=${DOMAIN_NAME} \ + --restart=unless-stopped \ + ${PROJECT_NAME}:latest + + [[ $? -ne 0 ]] && return 1 + + docker network connect ${DB_NETWORK} ${PROJECT_NAME} + [[ $? -ne 0 ]] && return 1 + + docker network connect ${PROXY_NETWORK} ${PROJECT_NAME} + [[ $? -ne 0 ]] && return 1 + + __ask_to_start +} + +"$@" diff --git a/common.shinc b/common.shinc new file mode 100644 index 0000000..fe4a7d0 --- /dev/null +++ b/common.shinc @@ -0,0 +1,104 @@ +#!/usr/bin/env bash + +REQUIRED_VARIABLES=(PROJECT_NAME SCRIPT_PATH IMAGE_NAME IMAGE_VERSION) + +for _var in ${REQUIRED_VARIABLES[@]}; do + if [ -z ${!_var+x} ]; then + echo "Missing definition of variable: ${_var}" + exit 2 + fi +done + +source ${SCRIPT_PATH}/../settings.shinc +source ${SCRIPT_PATH}/../functions.shinc + +# +# Commonly used functions +# + +stop() { + __msg "Stopping container... " 0 no + + STATUS=$(docker stop ${PROJECT_NAME} > /dev/null 2>&1) + if [[ $? -ne 0 ]]; then + __err "${STATUS}" + return 1 + else + __success "[ok]" + fi + + return 0 +} + +start() { + __msg "Starting container... " 0 no + STATUS=$(docker start ${PROJECT_NAME} 2>&1) + if [[ $? -ne 0 ]]; then + __err "${STATUS}" + return 1 + else + __success "[ok]" + fi + + return 0 +} + +remove() { + stop + + __msg "Removing container... " 0 no + STATUS=$(docker rm ${PROJECT_NAME} 2>&1) + if [[ $? -ne 0 ]]; then + __err "${STATUS}" + return 1 + else + __success "[ok]" + fi + + return 0 +} + + +logs() { + docker logs "$@" ${PROJECT_NAME} + return $? +} + +exec() { + # Test if bash is available + docker exec -it ${PROJECT_NAME} /bin/bash -c "echo 'test'" > /dev/null 2>&1 + + if [[ $? -eq 0 ]]; then + docker exec -it ${PROJECT_NAME} /bin/bash + return $? + fi + + # Test if bash is available + docker exec -it ${PROJECT_NAME} /bin/sh -c "echo 'test'" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + docker exec -it ${PROJECT_NAME} /bin/sh + + return $? + fi + + __err "Could not find either sh or bash in the container" + return 1 +} + +help() { + echo "┌──────────────────────────────────────────────────────────────┐" + echo "│ Available commands │" + echo "└──────────────────────────────────────────────────────────────┘" + echo -e -n "$LOG" + echo " > init - Initializes the environment" + echo " > start - Start containers" + echo " > stop - Stops containers" + echo " > remove - Removes traces of the container" + echo " > logs - Shows logs" + echo " > exec - Enters the containers tty" + echo " > help - Display this help" + echo -e -n "${NORMAL}" + echo "└──────────────────────────────────────────────────────────────┘" + + return 0 +} diff --git a/functions.shinc b/functions.shinc new file mode 100644 index 0000000..c0a701e --- /dev/null +++ b/functions.shinc @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + + +# Indent output by 2 x {n} spaces +__indent() { + if [[ $1 != "" ]] && [[ $1 -ne 0 ]]; then + printf ' %0.s' {1..$1} + fi +} + +__newLine() { + if [[ $1 = "" ]]; then + echo + fi +} + +__msg() { + echo -ne "${C_WHITE}" + __indent $2 + echo -ne "$1${C_NONE}" + __newLine $3 +} + +__warn() { + echo -ne "${C_WARN}" + __indent $2 + echo -ne "$1${C_NONE}" + __newLine $3 +} + +__success() { + echo -ne "${C_GREEN}" + __indent $2 + echo -ne "$1${C_NONE}" + __newLine $3 +} + +__err() { + echo -ne "${C_ERR}" + __indent $2 + echo -ne "$1${C_NONE}" + __newLine $3 +} + + +# +# Internal functions +# + +__createNetworks() { + for _var in ${NETWORKS[@]}; do + if [[ ${_var} = "" ]]; then + continue + fi + docker network inspect ${_var} > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + # If it does not, create it + __warn "The network ${_var} does not exist. Creating... " 1 no + STATUS=$(docker network create ${_var} 2>&1) + if [[ $? -ne 0 ]]; then + __err "${STATUS}" + return 117 + else + __success "[ok]" + fi + fi + done + return 0 +} + + +init() { + __err "The init function was not overridden" + + return 137 +} + +__init() { + __msg "Initializing ${PROJECT_NAME}" + + # Make sure we have the latest build + docker pull ${IMAGE_NAME}:${IMAGE_VERSION} + + return $? +} + +__build() { + docker build \ + --build-arg IMAGE_NAME=${IMAGE_NAME} \ + --build-arg IMAGE_VERSION=${IMAGE_VERSION} \ + -t ${PROJECT_NAME}:latest \ + ${SCRIPT_PATH} + + return $? +} + + +__ask_to_start() { + __success "Start the container? [(y)/n]... " + read START + + if [[ ${START} != "" ]] && [[ ${START} != "y" ]] && [[ ${START} != "Y" ]]; then + return 0 + fi + + start + return $? +} diff --git a/mailhog/run.sh b/mailhog/run.sh new file mode 100755 index 0000000..74b64fd --- /dev/null +++ b/mailhog/run.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P ) + +IMAGE_NAME="mailhog/mailhog" +IMAGE_VERSION="latest" + +PROJECT_NAME=mailhog + + +source ${SCRIPT_PATH}/../common.shinc + +# +# Project specific variables +# + +SMTP_PORT=1025 +WEB_PORT=8025 +DOMAIN_NAME="mailhog.loc" + + +init() { + __init + + NETWORKS=(${SMTP_NETWORK} ${PROXY_NETWORK}) + __createNetworks + + docker create \ + --name ${PROJECT_NAME} \ + -e VIRTUAL_HOST=${DOMAIN_NAME} \ + -e VIRTUAL_PORT=${WEB_PORT} \ + -p ${SMTP_PORT}:1025 \ + ${IMAGE_NAME}:${IMAGE_VERSION} + + [[ $? -ne 0 ]] && return 1 + + docker network connect ${SMTP_NETWORK} ${PROJECT_NAME} + [[ $? -ne 0 ]] && return 1 + + docker network connect ${PROXY_NETWORK} ${PROJECT_NAME} + [[ $? -ne 0 ]] && return 1 + + __ask_to_start +} + +"$@" + +exit $? diff --git a/maria-db/run.sh b/maria-db/run.sh new file mode 100755 index 0000000..882e845 --- /dev/null +++ b/maria-db/run.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P ) + +IMAGE_NAME="mariadb" +IMAGE_VERSION="10.1" + +PROJECT_NAME=maria-db + + +source ${SCRIPT_PATH}/../common.shinc + +# +# Project specific variables +# + +DB_VOLUME=mariadb_shared +BACKUP_DIR=${SCRIPT_PATH}/backup + +init() { + __init + + # Create networks + NETWORKS=(${DB_NETWORK}) + __createNetworks + + # Create the database container + docker create \ + --name ${PROJECT_NAME} \ + --restart=unless-stopped \ + -v ${DB_VOLUME}:/var/lib/mysql \ + -e MYSQL_ROOT_PASSWORD=${DB_PASSWORD} \ + --net ${DB_NETWORK} \ + ${IMAGE_NAME}:${IMAGE_VERSION} + + [[ $? -ne 0 ]] && return 1 + + __ask_to_start +} + +# Used for exporting the whole database filesystem +export() { + stop + local user=$(id -u) + + mkdir -p ${BACKUP_DIR} && \ + docker run --rm --volumes-from ${PROJECT_NAME} -v ${BACKUP_DIR}:/backup busybox tar cvf /backup/backup.tar /var/lib/mysql && chown -R ${user}:${user} ./backup + if [[ $? -ne 0 ]]; then + __err "Could not create the backup" + exit 2 + fi + + start + + return $? +} + +# Used for importing the whole database filesystem +import() { + stop + docker run --rm --volumes-from ${PROJECT_NAME} -v ${BACKUP_DIR}:/backup busybox sh -c "rm -rf /var/lib/mysql/* && tar xvf /backup/backup.tar --directory /" + start + + return $? +} + +"$@" + +exit $? diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..8981c5d --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,5 @@ +ARG IMAGE_NAME +ARG IMAGE_VERSION +FROM ${IMAGE_NAME}:${IMAGE_VERSION} + +COPY conf.d/ /etc/nginx/conf.d/ diff --git a/nginx/conf.d/proxy_timeout.conf b/nginx/conf.d/proxy_timeout.conf new file mode 100644 index 0000000..646b8e8 --- /dev/null +++ b/nginx/conf.d/proxy_timeout.conf @@ -0,0 +1,4 @@ +proxy_connect_timeout 300; +proxy_send_timeout 300; +proxy_read_timeout 300; +send_timeout 300; diff --git a/nginx/conf.d/request_size.conf b/nginx/conf.d/request_size.conf new file mode 100644 index 0000000..802443d --- /dev/null +++ b/nginx/conf.d/request_size.conf @@ -0,0 +1 @@ +client_max_body_size 2000m; diff --git a/nginx/run.sh b/nginx/run.sh new file mode 100755 index 0000000..3bf2604 --- /dev/null +++ b/nginx/run.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P ) + +IMAGE_NAME="jwilder/nginx-proxy" +IMAGE_VERSION="latest" + +PROJECT_NAME=nginx-proxy + + +source ${SCRIPT_PATH}/../common.shinc + +# +# Project specific variables +# + +init() { + __init + + NETWORKS=(${PROXY_NETWORK}) + __createNetworks + + __build || return $? + + # Create the nginx-proxy container + docker create \ + --name ${PROJECT_NAME} \ + -v /var/run/docker.sock:/tmp/docker.sock:ro \ + --restart=unless-stopped \ + -p 80:80 \ + -p 443:443 \ + --net ${PROXY_NETWORK} \ + ${PROJECT_NAME}:latest + + [[ $? -ne 0 ]] && return 1 + + __ask_to_start +} + +fix() { + local upperDir=$(docker inspect ${PROJECT_NAME} --format "{{.GraphDriver.Data.UpperDir}}") + [[ $? -ne 0 ]] && __err "Could not determine the nginx proxy filesystem path" && return 2 + + stop + sudo rm $(docker inspect ${PROJECT_NAME} --format "{{.GraphDriver.Data.UpperDir}}")/etc/nginx/conf.d/default.conf + + if [[ $? -eq 0 ]]; then + __success "Succesfully removed the nginx configuration, restart the container" + return 0 + else + __err "Could not remove the configuration file" + return 1 + fi +} + +"$@" + +exit $? diff --git a/pgsql-db/run.sh b/pgsql-db/run.sh new file mode 100755 index 0000000..7cbe7ed --- /dev/null +++ b/pgsql-db/run.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P ) + +IMAGE_NAME="mdillon/postgis" +IMAGE_VERSION="9.5" + +PROJECT_NAME=pgsql-db + + +source ${SCRIPT_PATH}/../common.shinc + +# +# Project specific variables +# + +DB_VOLUME=pgsql_persistent +PORT=5432 + +init() { + __init + + NETWORKS=(${DB_NETWORK}) + __createNetworks + + __msg "Removing old postgres bin volume..." + docker volume rm -f ${PGSQL_DB_BIN} && + docker create \ + --name ${PROJECT_NAME} \ + --restart=unless-stopped \ + -v ${DB_VOLUME}:/var/lib/postgresql/data \ + -v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \ + -e POSTGRES_USER=${DB_USER} \ + -e POSTGRES_PASSWORD=${DB_PASSWORD} \ + --net ${DB_NETWORK} \ + -p ${PORT}:5432 \ + ${IMAGE_NAME}:${IMAGE_VERSION} + + [[ $? -ne 0 ]] && return 1 + + __ask_to_start +} + +"$@" + +exit $? diff --git a/settings.shinc b/settings.shinc new file mode 100644 index 0000000..8f2aee9 --- /dev/null +++ b/settings.shinc @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# +# Networks +# + +# Reverse proxy +PROXY_NETWORK="web-server" + +# Databases +DB_NETWORK="database" + +# Mail +SMTP_NETWORK="smtp" + + + + +# +# Database settings +# + +DB_USER="root" +DB_PASSWORD="megaheslo" + +PGSQL_DB_BIN="postgres_bin_dir" + + + + +# +# Colors +# + +C_NONE="\033[0m" # Reset formating +C_ERR="\033[41;1m" # Red +C_WARN="\033[1;33m" # Yellow +C_HIGHLIGHT="\033[1;95m" # Purple +C_LOG="\033[1;3;36m" # Cyan +C_GREEN="\033[32m" +C_WHITE="\033[1;37m"