From 3b766d46bd627ce788a307de7805184b7830ab71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Colombaro?= Date: Mon, 22 Feb 2021 19:30:48 +0100 Subject: [PATCH] Move ENV_FILE usage to config file Ref https://github.com/docker-library/official-images/pull/9068#issuecomment-725748134 --- apache/config-docker.php | 25 ++++++++++++++++++------- apache/docker-entrypoint.sh | 30 ------------------------------ config-docker.php | 25 ++++++++++++++++++------- docker-entrypoint.sh | 30 ------------------------------ fpm-alpine/config-docker.php | 25 ++++++++++++++++++------- fpm-alpine/docker-entrypoint.sh | 30 ------------------------------ fpm/config-docker.php | 25 ++++++++++++++++++------- fpm/docker-entrypoint.sh | 30 ------------------------------ 8 files changed, 72 insertions(+), 148 deletions(-) diff --git a/apache/config-docker.php b/apache/config-docker.php index f17c64c..802c410 100644 --- a/apache/config-docker.php +++ b/apache/config-docker.php @@ -3,25 +3,36 @@ * Edit this file with your own settings and save it as "config.php" */ +// a helper function to lookup "env_FILE", "env", then fallback +function getenv_docker(string $name, ?string $default = null): ?string { + if ($fileEnv = getenv($name . '_FILE')) { + return trim(file_get_contents($fileEnv)); + } + if ($value = getenv($name)) { + return $value; + } + return $default; +} + /* ** MySQL settings - You can get this info from your web host */ /** MySQL database username */ -define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' ); +define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') ); /** MySQL database password */ -define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') ); +define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') ); /** The name of the database for YOURLS */ -define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' ); +define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */ -define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' ); +define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') ); /** MySQL tables prefix */ -define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' ); +define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') ); /* ** Site options @@ -29,7 +40,7 @@ /** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */ -define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' ); +define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') ); /** Server timezone GMT offset */ define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 ); @@ -57,7 +68,7 @@ ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information */ $yourls_user_passwords = [ - getenv('YOURLS_USER') => getenv('YOURLS_PASS'), + getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'), ]; /** Debug mode to output some internal information diff --git a/apache/docker-entrypoint.sh b/apache/docker-entrypoint.sh index 0b4426a..3387443 100755 --- a/apache/docker-entrypoint.sh +++ b/apache/docker-entrypoint.sh @@ -1,36 +1,6 @@ #!/bin/bash set -euo pipefail -# usage: file_env VAR [DEFAULT] -# ie: file_env 'XYZ_DB_PASSWORD' 'example' -# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of -# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) -file_env() { - local var="$1" - local fileVar="${var}_FILE" - local def="${2:-}" - if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then - printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar" - fi - local val="$def" - if [ "${!var:-}" ]; then - val="${!var}" - elif [ "${!fileVar:-}" ]; then - val="$(< "${!fileVar}")" - fi - export "$var"="$val" - unset "$fileVar" -} - -file_env 'YOURLS_DB_HOST' -file_env 'YOURLS_DB_USER' -file_env 'YOURLS_DB_PASS' -file_env 'YOURLS_DB_NAME' -file_env 'YOURLS_DB_PREFIX' -file_env 'YOURLS_SITE' -file_env 'YOURLS_USER' -file_env 'YOURLS_PASS' - if [ ! -e /var/www/html/yourls-loader.php ]; then tar cf - --one-file-system -C /usr/src/yourls . | tar xf - chown -R www-data:www-data /var/www/html diff --git a/config-docker.php b/config-docker.php index f17c64c..802c410 100644 --- a/config-docker.php +++ b/config-docker.php @@ -3,25 +3,36 @@ * Edit this file with your own settings and save it as "config.php" */ +// a helper function to lookup "env_FILE", "env", then fallback +function getenv_docker(string $name, ?string $default = null): ?string { + if ($fileEnv = getenv($name . '_FILE')) { + return trim(file_get_contents($fileEnv)); + } + if ($value = getenv($name)) { + return $value; + } + return $default; +} + /* ** MySQL settings - You can get this info from your web host */ /** MySQL database username */ -define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' ); +define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') ); /** MySQL database password */ -define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') ); +define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') ); /** The name of the database for YOURLS */ -define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' ); +define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */ -define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' ); +define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') ); /** MySQL tables prefix */ -define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' ); +define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') ); /* ** Site options @@ -29,7 +40,7 @@ /** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */ -define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' ); +define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') ); /** Server timezone GMT offset */ define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 ); @@ -57,7 +68,7 @@ ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information */ $yourls_user_passwords = [ - getenv('YOURLS_USER') => getenv('YOURLS_PASS'), + getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'), ]; /** Debug mode to output some internal information diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 0b4426a..3387443 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,36 +1,6 @@ #!/bin/bash set -euo pipefail -# usage: file_env VAR [DEFAULT] -# ie: file_env 'XYZ_DB_PASSWORD' 'example' -# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of -# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) -file_env() { - local var="$1" - local fileVar="${var}_FILE" - local def="${2:-}" - if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then - printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar" - fi - local val="$def" - if [ "${!var:-}" ]; then - val="${!var}" - elif [ "${!fileVar:-}" ]; then - val="$(< "${!fileVar}")" - fi - export "$var"="$val" - unset "$fileVar" -} - -file_env 'YOURLS_DB_HOST' -file_env 'YOURLS_DB_USER' -file_env 'YOURLS_DB_PASS' -file_env 'YOURLS_DB_NAME' -file_env 'YOURLS_DB_PREFIX' -file_env 'YOURLS_SITE' -file_env 'YOURLS_USER' -file_env 'YOURLS_PASS' - if [ ! -e /var/www/html/yourls-loader.php ]; then tar cf - --one-file-system -C /usr/src/yourls . | tar xf - chown -R www-data:www-data /var/www/html diff --git a/fpm-alpine/config-docker.php b/fpm-alpine/config-docker.php index f17c64c..802c410 100644 --- a/fpm-alpine/config-docker.php +++ b/fpm-alpine/config-docker.php @@ -3,25 +3,36 @@ * Edit this file with your own settings and save it as "config.php" */ +// a helper function to lookup "env_FILE", "env", then fallback +function getenv_docker(string $name, ?string $default = null): ?string { + if ($fileEnv = getenv($name . '_FILE')) { + return trim(file_get_contents($fileEnv)); + } + if ($value = getenv($name)) { + return $value; + } + return $default; +} + /* ** MySQL settings - You can get this info from your web host */ /** MySQL database username */ -define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' ); +define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') ); /** MySQL database password */ -define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') ); +define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') ); /** The name of the database for YOURLS */ -define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' ); +define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */ -define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' ); +define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') ); /** MySQL tables prefix */ -define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' ); +define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') ); /* ** Site options @@ -29,7 +40,7 @@ /** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */ -define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' ); +define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') ); /** Server timezone GMT offset */ define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 ); @@ -57,7 +68,7 @@ ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information */ $yourls_user_passwords = [ - getenv('YOURLS_USER') => getenv('YOURLS_PASS'), + getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'), ]; /** Debug mode to output some internal information diff --git a/fpm-alpine/docker-entrypoint.sh b/fpm-alpine/docker-entrypoint.sh index 0b4426a..3387443 100755 --- a/fpm-alpine/docker-entrypoint.sh +++ b/fpm-alpine/docker-entrypoint.sh @@ -1,36 +1,6 @@ #!/bin/bash set -euo pipefail -# usage: file_env VAR [DEFAULT] -# ie: file_env 'XYZ_DB_PASSWORD' 'example' -# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of -# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) -file_env() { - local var="$1" - local fileVar="${var}_FILE" - local def="${2:-}" - if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then - printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar" - fi - local val="$def" - if [ "${!var:-}" ]; then - val="${!var}" - elif [ "${!fileVar:-}" ]; then - val="$(< "${!fileVar}")" - fi - export "$var"="$val" - unset "$fileVar" -} - -file_env 'YOURLS_DB_HOST' -file_env 'YOURLS_DB_USER' -file_env 'YOURLS_DB_PASS' -file_env 'YOURLS_DB_NAME' -file_env 'YOURLS_DB_PREFIX' -file_env 'YOURLS_SITE' -file_env 'YOURLS_USER' -file_env 'YOURLS_PASS' - if [ ! -e /var/www/html/yourls-loader.php ]; then tar cf - --one-file-system -C /usr/src/yourls . | tar xf - chown -R www-data:www-data /var/www/html diff --git a/fpm/config-docker.php b/fpm/config-docker.php index f17c64c..802c410 100644 --- a/fpm/config-docker.php +++ b/fpm/config-docker.php @@ -3,25 +3,36 @@ * Edit this file with your own settings and save it as "config.php" */ +// a helper function to lookup "env_FILE", "env", then fallback +function getenv_docker(string $name, ?string $default = null): ?string { + if ($fileEnv = getenv($name . '_FILE')) { + return trim(file_get_contents($fileEnv)); + } + if ($value = getenv($name)) { + return $value; + } + return $default; +} + /* ** MySQL settings - You can get this info from your web host */ /** MySQL database username */ -define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' ); +define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') ); /** MySQL database password */ -define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') ); +define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') ); /** The name of the database for YOURLS */ -define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' ); +define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */ -define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' ); +define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') ); /** MySQL tables prefix */ -define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' ); +define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') ); /* ** Site options @@ -29,7 +40,7 @@ /** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */ -define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' ); +define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') ); /** Server timezone GMT offset */ define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 ); @@ -57,7 +68,7 @@ ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information */ $yourls_user_passwords = [ - getenv('YOURLS_USER') => getenv('YOURLS_PASS'), + getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'), ]; /** Debug mode to output some internal information diff --git a/fpm/docker-entrypoint.sh b/fpm/docker-entrypoint.sh index 0b4426a..3387443 100755 --- a/fpm/docker-entrypoint.sh +++ b/fpm/docker-entrypoint.sh @@ -1,36 +1,6 @@ #!/bin/bash set -euo pipefail -# usage: file_env VAR [DEFAULT] -# ie: file_env 'XYZ_DB_PASSWORD' 'example' -# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of -# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) -file_env() { - local var="$1" - local fileVar="${var}_FILE" - local def="${2:-}" - if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then - printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar" - fi - local val="$def" - if [ "${!var:-}" ]; then - val="${!var}" - elif [ "${!fileVar:-}" ]; then - val="$(< "${!fileVar}")" - fi - export "$var"="$val" - unset "$fileVar" -} - -file_env 'YOURLS_DB_HOST' -file_env 'YOURLS_DB_USER' -file_env 'YOURLS_DB_PASS' -file_env 'YOURLS_DB_NAME' -file_env 'YOURLS_DB_PREFIX' -file_env 'YOURLS_SITE' -file_env 'YOURLS_USER' -file_env 'YOURLS_PASS' - if [ ! -e /var/www/html/yourls-loader.php ]; then tar cf - --one-file-system -C /usr/src/yourls . | tar xf - chown -R www-data:www-data /var/www/html