Skip to content

Extract host and port from DB_HOST also support DB_PORT to test connection instead of only use default port 3306 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ services:
- PGID=1000
- APP_URL=
- DB_HOST=bookstack_db
- DB_PORT=3306
- DB_USER=bookstack
- DB_PASS=<yourdbpass>
- DB_DATABASE=bookstackapp
Expand Down Expand Up @@ -137,6 +138,7 @@ docker run -d \
-e PGID=1000 \
-e APP_URL= \
-e DB_HOST=<yourdbhost> \
-e DB_PORT=<yourdbport> \
-e DB_USER=<yourdbuser> \
-e DB_PASS=<yourdbpass> \
-e DB_DATABASE=bookstackapp \
Expand All @@ -157,6 +159,7 @@ Container images are configured using parameters passed at runtime (such as thos
| `-e PGID=1000` | for GroupID - see below for explanation |
| `-e APP_URL=` | for specifying the IP:port or URL your application will be accessed on (ie. `http://192.168.1.1:6875` or `https://bookstack.mydomain.com` |
| `-e DB_HOST=<yourdbhost>` | for specifying the database host |
| `-e DB_PORT=<yourdbport>` | for specifying the database port if not default 3306 |
| `-e DB_USER=<yourdbuser>` | for specifying the database user |
| `-e DB_PASS=<yourdbpass>` | for specifying the database password |
| `-e DB_DATABASE=bookstackapp` | for specifying the database to be used |
Expand Down
34 changes: 27 additions & 7 deletions root/etc/cont-init.d/50-config
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,20 @@ if [ "${DB_USER}" ];
then
echo "Running config - db_user set"
ESCAPED_PASSWORD=$(sed -E 's/('\'')/\\\1/g' <<< $DB_PASS)
sed -i "s/DB_HOST=localhost/DB_HOST=${DB_HOST}/g" /config/www/.env
sed -i "s/DB_DATABASE=database_database/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
sed -i "s/DB_USERNAME=database_username/DB_USERNAME=${DB_USER}/g" /config/www/.env
sed -i "s/DB_PASSWORD=database_user_password/DB_PASSWORD=${ESCAPED_PASSWORD}/g" /config/www/.env
sed -i "s/^DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
sed -i "s/^DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD=${ESCAPED_PASSWORD}/g" /config/www/.env

if [ -n "${DB_PORT}" ]; then
if ! grep -xq "^DB_PORT=.*" /config/www/.env; then
# add line DB_PORT=3306 to /config/www/.env because current /app/www/.env.example dont have it
sed -i "/^DB_HOST=.*/a DB_PORT=${DB_PORT}" /config/www/.env
echo "**** Insert DB_PORT=${DB_PORT} into /config/www/.env ****"
else
sed -i "s/^DB_PORT=.*/DB_PORT=${DB_PORT}/g" /config/www/.env
fi
fi
fi

# set appurl
Expand Down Expand Up @@ -96,11 +106,21 @@ if ! grep -qx '^post_max_size.*$' /config/php/php-local.ini; then
echo 'post_max_size = 100M' >> /config/php/php-local.ini
fi

# extract actual host and port from DB_HOST endpoint format, not support IPv6
# DB_HOST enpoint 'domainIp:port' or 'domainIp'
# DB_HOST_ONLY drop ':port' portion
# DB_PORT_ONLY drop host_only portion, remains '' or ':port'
# DB_PORT_ONLY drop ':' if any, remain '' or 'port'
# ${DB_PORT_ONLY:-${DB_PORT:-3306}} use DB_PORT if no port provided in DB_HOST, use default 3306 if not provide DB_PORT
DB_HOST_ONLY=${DB_HOST%:*}
DB_PORT_ONLY=${DB_HOST#$DB_HOST_ONLY}
DB_PORT_ONLY=${DB_PORT_ONLY#:}

# check for the mysql endpoint for 30 seconds
END=$((SECONDS+30))
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST+x}" ]; do
if /usr/bin/nc -z ${DB_HOST} 3306; then
if [ ! -z "$(/usr/bin/nc -w1 ${DB_HOST} 3306)" ]; then
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST_ONLY+x}" ]; do
if /usr/bin/nc -z ${DB_HOST_ONLY} ${DB_PORT_ONLY:-${DB_PORT:-3306}}; then
if [ ! -z "$(/usr/bin/nc -w1 ${DB_HOST_ONLY} ${DB_PORT_ONLY:-${DB_PORT:-3306}})" ]; then
if [ ! -z "${RUN}" ]; then
break
fi
Expand Down