Skip to content

Instantly share code, notes, and snippets.

@sseidenthal
Last active February 27, 2019 15:28
Show Gist options
  • Save sseidenthal/750e24119c91c322c8506168e6476e78 to your computer and use it in GitHub Desktop.
Save sseidenthal/750e24119c91c322c8506168e6476e78 to your computer and use it in GitHub Desktop.
This is a custom entrypoint for an php:7.1-apache image, it might work in a similar way with other base images but it has not been tested # # what it does # - it parses a parameters file *CONFIG_FILE* of your project an looks for variable placeholders # - it checks if any _FILE appended environment variables exist, if so it puts the content of t…
#!/bin/bash
# Maintainer Steve Seidenthal <[email protected]>
#
# This is a custom entrypoint for an php:7.1-apache image, it might work in a similar way with other base images but it
# has not been tested
#
# what it does
# - it parses a parameters file *CONFIG_FILE* of your project an looks for variable placeholders
# - it checks if any _FILE appended environment variables exist, if so it puts the content of the file in the variable
# - it makes sure all needed variables (as defined in CONFIG_FILE) exist
# - it prevents the container to start if any needed variable is unset
#
# **** PLEASE SCROLL DOWN TO CONFIG_FILE, IGNORE ALL ABOVE AN DO NOT CHANGE ****
set -euo pipefail
export ERRORS=0
# - looking for environment variables in CONFIG_FILE such as {{MY_VARIABLE}}
# - DO NOT TOUCH !
parse_placeholders() {
variables=(`tail $CONFIG_FILE | grep -oh "{{.*}}"`)
for variable in "${variables[@]}"
do
name=(`echo $variable | sed 's/}//g' | sed 's/{//g'`)
analyze_environment_variables "$name"
done
if [ ${ERRORS} -eq 1 ]; then
echo "*********************************************************************************"
echo "you are using environment variables which are not set ! please see the above list"
echo "*********************************************************************************"
exit
fi
}
# - parsing _FILE variable and ensuring environment variables exist
# - DO NOT TOUCH !
analyze_environment_variables() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
local type="env"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
local type="secret"
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
if [ -z "${!var:-}" ]
then
export ERRORS=1
echo " - $var has not been found"
else
echo " - $var found as $type"
fi
}
# - specify the CONFIG_FILE file in which variables should be replaced, syntax for a variable is {{MY_VARIABLE_NAME}}
CONFIG_FILE='/var/www/html/config/parameters.php'
# - make sure all variables defined in CONFIG_FILE exist as environment variable
# - parsing _FILE variables, typically secrets
# - DO NOT TOUCH !
parse_placeholders
# - use sed to replace the placeholders inside your configuration file by the actual content of the variable
# - you need to adapt these lines to your needs
sed -i -e "s@{{UPLOAD_FOLDER}}@${UPLOAD_FOLDER}@g" ${CONFIG_FILE}
sed -i -e "s@{{DATABASE_NAME}}@${DATABASE_NAME}@g" ${CONFIG_FILE}
sed -i -e "s@{{DATABASE_PORT}}@${DATABASE_PORT}@g" ${CONFIG_FILE}
sed -i -e "s@{{DATABASE_HOST}}@${DATABASE_HOST}@g" ${CONFIG_FILE}
sed -i -e "s@{{DATABASE_USERNAME}}@${DATABASE_USERNAME}@g" ${CONFIG_FILE}
sed -i -e "s@{{DATABASE_PASSWORD}}@${DATABASE_PASSWORD}@g" ${CONFIG_FILE}
# - starting apache process
# - DO NOT TOUCH !
docker-php-entrypoint
apache2-foreground
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment