Skip to content
Snippets Groups Projects
Commit 2fdb5055 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'detect-rootless-docker' into 'master'

docker init script: added start of mysql in the database container

See merge request !23
parents ba53455b 53a124d6
No related branches found
No related tags found
1 merge request!23docker init script: added start of mysql in the database container
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
# 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions) # 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions)
# 2024-10-16 v1.20 <axel.hahn@unibe.ch> add db import and export # 2024-10-16 v1.20 <axel.hahn@unibe.ch> add db import and export
# 2024-10-25 v1.21 <axel.hahn@unibe.ch> create missing subdir dbdumps # 2024-10-25 v1.21 <axel.hahn@unibe.ch> create missing subdir dbdumps
# 2024-10-30 v1.22 <axel.hahn@unibe.ch> added: Open Mysql client in container
# 2024-10-30 v1.23 <axel.hahn@unibe.ch> added: show menu hints why some menu items are visible
# ====================================================================== # ======================================================================
cd "$( dirname "$0" )" || exit 1 cd "$( dirname "$0" )" || exit 1
...@@ -39,7 +41,7 @@ _self=$( basename "$0" ) ...@@ -39,7 +41,7 @@ _self=$( basename "$0" )
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "${_self}.cfg" || exit 1 . "${_self}.cfg" || exit 1
_version="1.21" _version="1.23"
# git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git
selfgitrepo="docker-php-starterkit.git" selfgitrepo="docker-php-starterkit.git"
...@@ -66,6 +68,7 @@ DC_CONFIG_CHANGED=0 ...@@ -66,6 +68,7 @@ DC_CONFIG_CHANGED=0
DC_WEB_URL="" DC_WEB_URL=""
DC_DUMP_DIR=dbdumps DC_DUMP_DIR=dbdumps
DC_SHOW_MENUHINTS=0
isDockerRootless=0 isDockerRootless=0
ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1 ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1
...@@ -112,6 +115,10 @@ fi ...@@ -112,6 +115,10 @@ fi
} }
# Get web url of the application
# It is for support of Nginx Docker Proxy
# https://github.com/axelhahn/nginx-docker-proxy
# It returns http://localhost:<port> or a https://<appname>
function _getWebUrl(){ function _getWebUrl(){
DC_WEB_URL="$frontendurl" DC_WEB_URL="$frontendurl"
grep -q "${APP_NAME}-server" /etc/hosts && DC_WEB_URL="https://${APP_NAME}-server/" grep -q "${APP_NAME}-server" /etc/hosts && DC_WEB_URL="https://${APP_NAME}-server/"
...@@ -137,6 +144,15 @@ function _key(){ ...@@ -137,6 +144,15 @@ function _key(){
echo -en "\e[4;7m ${1} \e[0m" echo -en "\e[4;7m ${1} \e[0m"
} }
# helper for menu: show hint text
# param int FLag _bAll (i true the txt will be hidden)
# param string message to show
function menuhint(){
local _bAll="$1"
shift 1
test $DC_SHOW_MENUHINTS -ne 0 && test "$_bAll" -eq "0" && ( echo -e "$fgBlue $*$fgReset" )
}
# show menu in interactive mode and list keys in help with param -h # show menu in interactive mode and list keys in help with param -h
# param string optional: set to "all" to show all menu items # param string optional: set to "all" to show all menu items
function showMenu(){ function showMenu(){
...@@ -148,44 +164,60 @@ function showMenu(){ ...@@ -148,44 +164,60 @@ function showMenu(){
echo echo
if [ $DC_REPO -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_REPO -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Git data of starterkit were found"
echo "${_spacer}$( _key g ) - remove git data of starterkit" echo "${_spacer}$( _key g ) - remove git data of starterkit"
echo echo
fi fi
if [ $isDockerRootless -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $isDockerRootless -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Beause rootless docker was found"
echo "${_spacer}$( _key i ) - init application: set permissions" echo "${_spacer}$( _key i ) - init application: set permissions"
echo
fi fi
if [ $DC_CONFIG_CHANGED -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Config was changed"
echo "${_spacer}$( _key t ) - generate files from templates" echo "${_spacer}$( _key t ) - generate files from templates"
echo
fi fi
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Config is unchanged"
echo "${_spacer}$( _key T ) - remove generated files" echo "${_spacer}$( _key T ) - remove generated files"
fi
echo echo
fi
if [ $DC_WEB_UP -eq 0 ] || [ $DC_DB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 0 ] || [ $DC_DB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "A container is down and config is unchanged"
echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d" echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d"
echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build" echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build"
echo echo
echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f" echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f"
echo
fi fi
fi fi
if [ $DC_WEB_UP -eq 1 ] || [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 1 ] || [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "${_spacer}A container is up"
echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop" echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop"
echo echo
echo "${_spacer}$( _key m ) - more infos" echo "${_spacer}$( _key m ) - more infos"
echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL" echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL"
echo "${_spacer}$( _key c ) - console (bash)" echo "${_spacer}$( _key c ) - console (bash)"
echo
fi fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Web container is up"
echo "${_spacer}$( _key p ) - console check with php linter" echo "${_spacer}$( _key p ) - console check with php linter"
echo
fi fi
if [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo
menuhint $_bAll "Database container is up"
echo "${_spacer}$( _key d ) - Dump container database" echo "${_spacer}$( _key d ) - Dump container database"
echo "${_spacer}$( _key D ) - Import Dump into container database" echo "${_spacer}$( _key D ) - Import Dump into container database"
fi echo "${_spacer}$( _key M ) - Open Mysql client in database container"
echo echo
fi
menuhint $_bAll "Always available"
echo "${_spacer}$( _key q ) - quit" echo "${_spacer}$( _key q ) - quit"
} }
...@@ -706,6 +738,10 @@ while true; do ...@@ -706,6 +738,10 @@ while true; do
h2 "DB tools :: import" h2 "DB tools :: import"
_dbImport _dbImport
;; ;;
M)
h2 "DB tools :: mysql client"
docker exec -it "${APP_NAME}-db" mysql -uroot -p${MYSQL_ROOT_PASS} "${MYSQL_DB}"
;;
o) o)
h2 "Open app ..." h2 "Open app ..."
xdg-open "$DC_WEB_URL" xdg-open "$DC_WEB_URL"
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2021-12-17 <axel.hahn@iml.unibe.ch> # 2021-12-17 <axel.hahn@iml.unibe.ch>
# 2024-10-30 <axel.hahn@unibe.ch> add DC_SHOW_MENUHINTS
# ====================================================================== # ======================================================================
APP_NAME=my_new_app APP_NAME=my_new_app
...@@ -47,6 +48,11 @@ MYSQL_USER=${APP_NAME} ...@@ -47,6 +48,11 @@ MYSQL_USER=${APP_NAME}
MYSQL_PASS=mypassword MYSQL_PASS=mypassword
MYSQL_DB=${APP_NAME} MYSQL_DB=${APP_NAME}
# ----------------------------------------------------------------------
# Settings for init.sh
# show hints for menu items in interactive mode
DC_SHOW_MENUHINTS=0
# ====================================================================== # ======================================================================
......
...@@ -129,6 +129,7 @@ APP_PORT | int | port number for your local machine that ...@@ -129,6 +129,7 @@ APP_PORT | int | port number for your local machine that
CUTTER_NO_DATABASE | const | do not change it; the string is used to cut templates from there if no database is used CUTTER_NO_DATABASE | const | do not change it; the string is used to cut templates from there if no database is used
DB_ADD | bool | flag: do you add a Mariadb container? default: false DB_ADD | bool | flag: do you add a Mariadb container? default: false
DB_PORT | int | port number for your local machine that will be mapped to port 3306 in the container; You can use it in a local db manager; remark: tha apache container needs 3306 and not the mapped port. DB_PORT | int | port number for your local machine that will be mapped to port 3306 in the container; You can use it in a local db manager; remark: tha apache container needs 3306 and not the mapped port.
DC_SHOW_MENUHINTS | int | Flag to show a notice why menu items are visible; default: 0 (off)
DOCKER_USER_UID | int | unix uid of the httpd service user in apache container; it's not needed to change; default: 33 DOCKER_USER_UID | int | unix uid of the httpd service user in apache container; it's not needed to change; default: 33
MYSQL_ALLOW_EMPTY_PASSWORD | int | flag: 0 MYSQL_ALLOW_EMPTY_PASSWORD | int | flag: 0
MYSQL_DB | string | name of database; defaut is app name ${APP_NAME} MYSQL_DB | string | name of database; defaut is app name ${APP_NAME}
......
...@@ -31,7 +31,6 @@ Optional changes: ...@@ -31,7 +31,6 @@ Optional changes:
# (2) run "docker-compose up" to startup # (2) run "docker-compose up" to startup
# #
# ====================================================================== # ======================================================================
version: '3.9'
networks: networks:
{{APP_NAME}}-network: {{APP_NAME}}-network:
......
...@@ -13,7 +13,7 @@ The script supports command line parameters to use it in scripts. Use `-h` to ge ...@@ -13,7 +13,7 @@ The script supports command line parameters to use it in scripts. Use `-h` to ge
```txt ```txt
INITIALIZER FOR DOCKER APP v1.20 INITIALIZER FOR DOCKER APP v1.22
A helper script written in Bash to bring up a PHP+Mysql application in docker. A helper script written in Bash to bring up a PHP+Mysql application in docker.
...@@ -49,12 +49,14 @@ MENU KEYS: ...@@ -49,12 +49,14 @@ MENU KEYS:
r - remove containers docker-compose rm -f r - remove containers docker-compose rm -f
s - shutdown containers docker-compose stop s - shutdown containers docker-compose stop
i - Import more into infos m - more infos
o - open app [my_new_app] http://localhost:8001/ o - open app [my_new_app] http://localhost:8001/
c - console (bash) c - console (bash)
p - console check with php linter p - console check with php linter
d - Dump container database d - Dump container database
D - Import Dump into container database D - Import Dump into container database
M - Open Mysql client in database container
q - quit q - quit
...@@ -153,3 +155,10 @@ You get a file selection of the `./dbdumps/` directory. ...@@ -153,3 +155,10 @@ You get a file selection of the `./dbdumps/` directory.
The selected sql or sql.gz file will be imported. The selected sql or sql.gz file will be imported.
**Hint**: With this feature you can import a dump from a live system into your dev environment too. Just copy its dump into `./dbdumps/`. **Hint**: With this feature you can import a dump from a live system into your dev environment too. Just copy its dump into `./dbdumps/`.
#### M - Open Mysql client in database container
With docker exec in the database container the mysql client `mysql` will be started with root credentials on the application database.
This is faster than opening a shell on the container and start `mysql -u root -p<password> <dbname>` where you have to search the credentials first.
Because of the usage of docker exec it works too when the database port is not exposed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment