diff --git a/docker/containers/db-server/mariadb/dot_my.cnf b/docker/containers/db-server/mariadb/dot_my.cnf new file mode 100644 index 0000000000000000000000000000000000000000..0e8e7cfd4b26a9ba2c09e3bc3b06cc8557eaa148 --- /dev/null +++ b/docker/containers/db-server/mariadb/dot_my.cnf @@ -0,0 +1,4 @@ +[client] +user=root +host=localhost +password=12345678 diff --git a/docker/containers/db-server/var_tmp_db-data/readme.txt b/docker/containers/db-server/var_tmp_db-data/readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..52632b9f96ef603257ce34e7b780608447a7479c --- /dev/null +++ b/docker/containers/db-server/var_tmp_db-data/readme.txt @@ -0,0 +1,7 @@ + +===== Info ===== + +content of `docker/containers/db_server/var_tmp_db-data/` +will be visible in the database container as `/var/tmp/db-data/`. + +It is a helper to transfer/ import sqldumps. diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index c01edc7832b3f720dfce00c51e8d49ff60cc7fdc..98a837cb10efa172cba3512f1aca530775c2a431 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,5 +1,5 @@ # -# GENERATED BY init.sh - template: ./templates/docker-compose.yml - 97c88229bd2b5099544c013052b8d9c3 +# GENERATED BY init.sh - template: ./templates/docker-compose.yml - d3f1d9971b8c651cfcec647a3de69105 # # ====================================================================== # diff --git a/docker/init.sh b/docker/init.sh index 060db24942ad10e1bb0f3abca96443d85f55d289..994a4d09a47ce1ac5609deed9b5edf23b68791e4 100755 --- a/docker/init.sh +++ b/docker/init.sh @@ -11,6 +11,9 @@ # 2022-12-20 v1.4 <axel.hahn@unibe.ch> replace fgrep with grep -F # 2023-03-06 v1.5 <www.axel-hahn.de> up with and without --build # 2023-08-17 v1.6 <www.axel-hahn.de> menu selection with single key (without return) +# 2023-11-10 v1.7 <axel.hahn@unibe.ch> replace docker-compose with "docker compose" +# 2023-11-13 v1.8 <axel.hahn@unibe.ch> UNDO "docker compose"; update infos +# 2023-11-15 v1.9 <axel.hahn@unibe.ch> add help; execute multiple actions by params; new menu item: open app # ====================================================================== cd $( dirname $0 ) @@ -19,7 +22,7 @@ cd $( dirname $0 ) # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git selfgitrepo="docker-php-starterkit.git" -_version="1.6" +_version="1.9" # ---------------------------------------------------------------------- # FUNCTIONS @@ -37,6 +40,61 @@ function h3(){ echo -e "\e[34m----- $*\e[0m" } +# show help for param -h +function showMenu(){ + echo " $( _key g ) - remove git data of starterkit" + echo + echo " $( _key i ) - init application: set permissions" + echo " $( _key t ) - generate files from templates" + echo " $( _key T ) - remove generated files" + echo + echo " $( _key u ) - startup containers docker-compose ... up -d" + echo " $( _key U ) - startup containers docker-compose ... up -d --build" + echo " $( _key s ) - shutdown containers docker-compose stop" + echo " $( _key r ) - remove containers docker-compose rm -f" + echo + echo " $( _key m ) - more infos" + echo " $( _key o ) - open app [${APP_NAME}] $frontendurl" + echo " $( _key c ) - console (bash)" + echo + echo " $( _key q ) - quit" +} +function showHelp(){ + local _self=$( basename "$0" ) + cat <<EOH +INITIALIZER FOR DOCKER APP v$_version + +A helper script written in Bash to bring up a PHP+Mysql application in docker. + +Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit +Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/ +License: GNU GPL 3.0 +(c) Institute for Medical Education; University of Bern + + +SYNTAX: + $_self [-h|-v] + $_self [menu key] + +OPTIONS: + -h show this help and exit + -v show version exit + +MENU KEYS: + In the interactive menu are some keys to init an action. + The same keys can be put as parameter to start this action. + You can add multiples keys to apply multiple actions. + +$( showMenu ) + +EXAMPLES: + + $_self starts interactive mode + $_self u bring up docker container(s) and stay in interactive mode + $_self i q set write permissions and quit + +EOH +} # function _gitinstall(){ # h2 "install/ update app from git repo ${gitrepo} in ${gittarget} ..." # test -d ${gittarget} && ( cd ${gittarget} && git pull ) @@ -189,6 +247,7 @@ function _removeGeneratedFiles(){ done } +# show running containers function _showContainers(){ local bLong=$1 h2 CONTAINERS @@ -200,7 +259,16 @@ function _showContainers(){ } -# a bit stupid ... i think I need to delete it. +# show urls for app container +function _showBrowserurl(){ + echo "In a web browser open:" + echo " $frontendurl" + if grep "${APP_NAME}-server" /etc/hosts >/dev/null; then + echo " https://${APP_NAME}-server/" + fi +} + +# detect + show ports and urls for app container and db container function _showInfos(){ _showContainers long h2 INFO @@ -212,19 +280,20 @@ function _showInfos(){ >/dev/tcp/localhost/${APP_PORT} 2>/dev/null && ( echo "OK, app port ${APP_PORT} is reachable" echo - echo "In a web browser open:" - echo " $frontendurl" + _showBrowserurl ) - h3 "Check database port" - >/dev/tcp/localhost/${DB_PORT} 2>/dev/null && ( - echo "OK, db port ${DB_PORT} is reachable" - echo + if [ "$DB_ADD" != "false" ]; then + h3 "Check database port" + >/dev/tcp/localhost/${DB_PORT} >/dev/null 2>&1 && ( + echo "OK, db port ${DB_PORT} is reachable" + echo + ) echo "In a local DB admin tool:" echo " host : localhost" echo " port : ${DB_PORT}" echo " user : root" echo " password: ${MYSQL_ROOT_PASS}" - ) + fi echo } @@ -235,39 +304,26 @@ function _key(){ # helper: wait for a return key function _wait(){ - echo -n "... press RETURN > "; read -r + echo -n "... press RETURN > "; read -r -t 15 } # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- -action=$1 +action=$1; shift 1 while true; do - echo - echo -e "\e[32m===== INITIALIZER FOR DOCKER APP [$APP_NAME] v$_version ===== \e[0m\n\r" if [ -z "$action" ]; then + echo + echo -e "\e[32m===== INITIALIZER FOR DOCKER APP [$APP_NAME] v$_version ===== \e[0m\n\r" + _showContainers - h2 MENU - echo " $( _key g ) - remove git data of starterkit" - echo - echo " $( _key i ) - init application: set permissions" - echo " $( _key t ) - generate files from templates" - echo " $( _key T ) - remove generated files" - echo - echo " $( _key u ) - startup containers docker-compose ... up -d" - echo " $( _key U ) - startup containers docker-compose ... up -d --build" - echo " $( _key s ) - shutdown containers docker-compose stop" - echo " $( _key r ) - remove containers docker-compose rm -f" - echo - echo " $( _key m ) - more infos" - echo " $( _key c ) - console (bash)" - echo - echo " $( _key q ) - quit" + h2 MENU + showMenu echo echo -n " select >" read -rn 1 action @@ -275,6 +331,8 @@ while true; do fi case "$action" in + "-h") showHelp; exit 0 ;; + "-v") echo $(basename $0) $_version; exit 0 ;; g) _removeGitdata ;; @@ -289,24 +347,19 @@ while true; do _removeGeneratedFiles rm -rf containers ;; - # not in the menu - # f) - # _removeGeneratedFiles - # _generateFiles - # _wait - # ;; m) _showInfos _wait ;; u|U) + h2 "Bring up..." dockerUp="docker-compose -p "$APP_NAME" --verbose up -d --remove-orphans" if [ "$action" = "U" ]; then dockerUp+=" --build" fi + echo "$dockerUp" if $dockerUp; then - echo "In a web browser:" - echo " $frontendurl" + _showBrowserurl else echo "ERROR: docker-compose up failed :-/" docker-compose -p "$APP_NAME" logs | tail @@ -316,24 +369,32 @@ while true; do _wait ;; s) + h2 "Stopping..." docker-compose -p "$APP_NAME" stop ;; r) + h2 "Removing..." docker-compose -p "$APP_NAME" rm -f ;; c) + h2 "Console" docker ps echo -n "id or name >" read dockerid test -z "$dockerid" || docker exec -it $dockerid /bin/bash ;; + o) + h2 "Open app ..." + xdg-open "$frontendurl" + ;; q) + h2 "Bye!" exit 0; ;; *) test -n "$action" && ( echo " ACTION FOR [$action] NOT IMPLEMENTED."; sleep 1 ) esac - action= + action=$1; shift 1 done diff --git a/docker/templates/db_data_readme.txt b/docker/templates/db_data_readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..759fb69b4e56dec3948ddb1bb870279b64cd6bbc --- /dev/null +++ b/docker/templates/db_data_readme.txt @@ -0,0 +1,8 @@ +# TARGET: docker/containers/db-server/var_tmp_db-data/readme.txt + +===== Info ===== + +content of `docker/containers/db_server/var_tmp_db-data/` +will be visible in the database container as `/var/tmp/db-data/`. + +It is a helper to transfer/ import sqldumps. diff --git a/docker/templates/docker-compose.yml b/docker/templates/docker-compose.yml index 0cdc088a13e05801f43a11cfab9881c8c3158075..0d2cfd480e28fae63ddbb235740d3bf3e3b4c653 100644 --- a/docker/templates/docker-compose.yml +++ b/docker/templates/docker-compose.yml @@ -74,8 +74,9 @@ services: MYSQL_PASSWORD: '${MYSQL_PASS}' MYSQL_DATABASE: '${MYSQL_DB}' volumes: - # - ./containers/db-server/db_data:/var/lib/mysql - ./containers/db-server/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf + - ./containers/db-server/mariadb/dot_my.cnf:/root/.my.cnf + - ./containers/db-server/var_tmp_db-data/:/var/tmp/db-data healthcheck: test: mysqladmin ping -h 127.0.0.1 -u root --password=$$MYSQL_ROOT_PASSWORD interval: 5s diff --git a/docker/templates/dot_my.cnf b/docker/templates/dot_my.cnf new file mode 100644 index 0000000000000000000000000000000000000000..27b254ae8ba835c6abd82e76e59ff450267f3fa9 --- /dev/null +++ b/docker/templates/dot_my.cnf @@ -0,0 +1,5 @@ +# TARGET: docker/containers/db-server/mariadb/dot_my.cnf +[client] +user=root +host=localhost +password={{MYSQL_ROOT_PASS}}