diff --git a/Dockerfile b/Dockerfile index 753135d..3c5d5e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -106,7 +106,7 @@ ENV APP_HOME_DIR=/usr/local/${APP_NAME} \ APP_DEF_DIR=/etc/${APP_NAME} ENV PATH="${APP_HOME_DIR}/bin:${PATH}" \ - LD_LIBRARY_PATH=${APP_HOME_DIR}/lib + LD_LIBRARY_PATH="${APP_HOME_DIR}/lib" LABEL \ "Version"="v${APP_VERSION}" \ @@ -141,6 +141,9 @@ VOLUME ["/srv/conf", "/srv/data", "/srv/datalog", "/srv/cert", "/var/log"] # 默认使用gosu切换为新建用户启动,必须保证端口在1024之上 EXPOSE 5432 +# 应用健康状态检查 +HEALTHCHECK CMD PGPASSWORD="${PG_POSTGRES_PASSWORD}" psql -h 127.0.0.1 -d postgres -U postgres -At -c "select version();" || exit 1 + # 容器初始化命令,默认存放在:/usr/local/bin/entry.sh ENTRYPOINT ["entry.sh"] diff --git a/Makefile b/Makefile index 5ebb830..ea8c14b 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,6 @@ build-arg:=--build-arg apt_source=tencent # 设置本地下载服务器路径,加速调试时的本地编译速度 local_ip:=`echo "en0 eth0" |xargs -n1 ip addr show 2>/dev/null|grep inet|grep -v 127.0.0.1|grep -v inet6|tr "/" " "|awk '{print $$2}'` build-arg+=--build-arg local_url=http://$(local_ip)/dist-files -#--build-arg local_url=http://192.168.1.187/dist-files/postgresql .PHONY: build build-debian build-alpine clean clearclean upgrade @@ -61,3 +60,4 @@ push: tag upgrade: @echo "Upgrade all images..." @docker images | grep 'colovu' | grep -v '' | grep -v "latest-" | awk '{print $$1":"$$2}' | sort -u | xargs -L 1 docker pull + diff --git a/alpine/Dockerfile b/alpine/Dockerfile index 3f79c6d..0411d2b 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -109,7 +109,7 @@ ENV APP_HOME_DIR=/usr/local/${APP_NAME} \ APP_DEF_DIR=/etc/${APP_NAME} ENV PATH="${APP_HOME_DIR}/bin:${PATH}" \ - LD_LIBRARY_PATH=${APP_HOME_DIR}/lib + LD_LIBRARY_PATH="${APP_HOME_DIR}/lib" LABEL \ "Version"="v${APP_VERSION}" \ @@ -144,6 +144,9 @@ VOLUME ["/srv/conf", "/srv/data", "/srv/datalog", "/srv/cert", "/var/log"] # 默认使用gosu切换为新建用户启动,必须保证端口在1024之上 EXPOSE 5432 +# 应用健康状态检查 +HEALTHCHECK CMD PGPASSWORD="${PG_POSTGRES_PASSWORD}" psql -h 127.0.0.1 -d postgres -U postgres -At -c "select version();" || exit 1 + # 容器初始化命令,默认存放在:/usr/local/bin/entry.sh ENTRYPOINT ["entry.sh"] diff --git a/alpine/customer/usr/local/bin/comm-env.sh b/alpine/customer/usr/local/bin/comm-env.sh index 30d614b..54892a7 100644 --- a/alpine/customer/usr/local/bin/comm-env.sh +++ b/alpine/customer/usr/local/bin/comm-env.sh @@ -22,7 +22,7 @@ for env_var in "${app_env_file_lists[@]}"; do unset "${file_env_var}" fi done -unset postgresql_env_vars +unset app_env_file_lists # 应用路径参数 export APP_HOME_DIR="/usr/local/${APP_NAME}" @@ -102,6 +102,7 @@ export PG_USERNAME="${PG_USERNAME:-postgres}" export PG_PASSWORD="${PG_PASSWORD:-}" export PG_DATABASE="${PG_DATABASE:-postgres}" # 使用自定义用户名(非"postgres")时的管理员密码 +[[ "${PG_USERNAME}" = "postgres" ]] && PG_POSTGRES_PASSWORD="${PG_PASSWORD}" export PG_POSTGRES_PASSWORD="${PG_POSTGRES_PASSWORD:-}" export PG_INITSCRIPTS_USERNAME="${PG_INITSCRIPTS_USERNAME:-${PG_USERNAME}}" export PG_INITSCRIPTS_PASSWORD="${PG_INITSCRIPTS_PASSWORD:-${PG_PASSWORD}}" diff --git a/alpine/customer/usr/local/bin/comm-postgresql.sh b/alpine/customer/usr/local/bin/comm-postgresql.sh index 37805f2..b4c9e23 100644 --- a/alpine/customer/usr/local/bin/comm-postgresql.sh +++ b/alpine/customer/usr/local/bin/comm-postgresql.sh @@ -48,7 +48,7 @@ postgresql_common_conf_set() { if grep -q "^#*\s*${key}" "$file" >/dev/null; then replace_in_file "$file" "^#*\s*${key}\s*=.*" "${key} = '${value}'" false else - echo "${property} = '${value}'" >>"$file" + echo "${key} = '${value}'" >>"$file" fi } @@ -191,7 +191,7 @@ postgresql_execute() { postgresql_configure_from_environment_variables() { LOG_D "Modify postgresql.conf with PG_CFG_* values..." for var in "${!PG_CFG_@}"; do - key="$(echo "$var" | sed -e 's/^PG_CFG_//g' | tr '[:upper:]' '[:lower:]')" + key="$(echo "$var" | sed -e 's/^PG_CFG_//g' | sed -e 's/___/-/g' | sed -e 's/__/./g' | tr '[:upper:]' '[:lower:]')" value="${!var}" postgresql_conf_set "$key" "$value" done @@ -202,8 +202,6 @@ postgresql_default_postgresql_config() { LOG_I "Modify postgresql.conf with default values..." [ ! -e "${PG_CONF_FILE}" ] && cp -rf "${APP_HOME_DIR}/share/postgresql.conf.sample" "${PG_CONF_FILE}" - - postgresql_configure_from_environment_variables postgresql_conf_set "logging_collector" "on" postgresql_conf_set "wal_level" "hot_standby" @@ -220,14 +218,10 @@ postgresql_default_postgresql_config() { [[ -n "${PG_SHARED_PRELOAD_LIBRARIES}" ]] && postgresql_conf_set "shared_preload_libraries" "${PG_SHARED_PRELOAD_LIBRARIES}" - # Update default value for 'include_dir' directive - # ref: https://github.com/postgres/postgres/commit/fb9c475597c245562a28d1e916b575ac4ec5c19f#diff-f5544d9b6d218cc9677524b454b41c60 - if ! grep include_dir "${PG_CONF_FILE}" > /dev/null; then - postgresql_error "include_dir line is not present in ${PG_CONF_FILE}. This may be due to a changes in a new version of PostgreSQL. Please check" - exit 1 - fi postgresql_conf_set "include_dir" "conf.d" mkdir -p "${APP_CONF_DIR}/conf.d" + + postgresql_configure_from_environment_variables } # 生成初始 pg_hba.conf 配置 diff --git a/alpine/customer/usr/local/bin/run.sh b/alpine/customer/usr/local/bin/run.sh index bdb6ea8..fcbb526 100755 --- a/alpine/customer/usr/local/bin/run.sh +++ b/alpine/customer/usr/local/bin/run.sh @@ -14,9 +14,9 @@ set -o pipefail LOG_I "** Processing run.sh **" -flags=("-D" "/srv/data/${APP_NAME}/data") +flags=("--config-file=${PG_CONF_FILE}" "--hba_file=${PG_HBA_FILE}") [[ -z "${APP_EXTRA_FLAGS:-}" ]] || flags=("${flags[@]}" "${APP_EXTRA_FLAGS[@]}") -START_COMMAND=("${APP_EXEC}") +START_COMMAND=("postgres") LOG_I "** Starting ${APP_NAME} **" if is_root; then diff --git a/customer/usr/local/bin/comm-env.sh b/customer/usr/local/bin/comm-env.sh index 30d614b..54892a7 100644 --- a/customer/usr/local/bin/comm-env.sh +++ b/customer/usr/local/bin/comm-env.sh @@ -22,7 +22,7 @@ for env_var in "${app_env_file_lists[@]}"; do unset "${file_env_var}" fi done -unset postgresql_env_vars +unset app_env_file_lists # 应用路径参数 export APP_HOME_DIR="/usr/local/${APP_NAME}" @@ -102,6 +102,7 @@ export PG_USERNAME="${PG_USERNAME:-postgres}" export PG_PASSWORD="${PG_PASSWORD:-}" export PG_DATABASE="${PG_DATABASE:-postgres}" # 使用自定义用户名(非"postgres")时的管理员密码 +[[ "${PG_USERNAME}" = "postgres" ]] && PG_POSTGRES_PASSWORD="${PG_PASSWORD}" export PG_POSTGRES_PASSWORD="${PG_POSTGRES_PASSWORD:-}" export PG_INITSCRIPTS_USERNAME="${PG_INITSCRIPTS_USERNAME:-${PG_USERNAME}}" export PG_INITSCRIPTS_PASSWORD="${PG_INITSCRIPTS_PASSWORD:-${PG_PASSWORD}}" diff --git a/customer/usr/local/bin/comm-postgresql.sh b/customer/usr/local/bin/comm-postgresql.sh index 37805f2..b4c9e23 100644 --- a/customer/usr/local/bin/comm-postgresql.sh +++ b/customer/usr/local/bin/comm-postgresql.sh @@ -48,7 +48,7 @@ postgresql_common_conf_set() { if grep -q "^#*\s*${key}" "$file" >/dev/null; then replace_in_file "$file" "^#*\s*${key}\s*=.*" "${key} = '${value}'" false else - echo "${property} = '${value}'" >>"$file" + echo "${key} = '${value}'" >>"$file" fi } @@ -191,7 +191,7 @@ postgresql_execute() { postgresql_configure_from_environment_variables() { LOG_D "Modify postgresql.conf with PG_CFG_* values..." for var in "${!PG_CFG_@}"; do - key="$(echo "$var" | sed -e 's/^PG_CFG_//g' | tr '[:upper:]' '[:lower:]')" + key="$(echo "$var" | sed -e 's/^PG_CFG_//g' | sed -e 's/___/-/g' | sed -e 's/__/./g' | tr '[:upper:]' '[:lower:]')" value="${!var}" postgresql_conf_set "$key" "$value" done @@ -202,8 +202,6 @@ postgresql_default_postgresql_config() { LOG_I "Modify postgresql.conf with default values..." [ ! -e "${PG_CONF_FILE}" ] && cp -rf "${APP_HOME_DIR}/share/postgresql.conf.sample" "${PG_CONF_FILE}" - - postgresql_configure_from_environment_variables postgresql_conf_set "logging_collector" "on" postgresql_conf_set "wal_level" "hot_standby" @@ -220,14 +218,10 @@ postgresql_default_postgresql_config() { [[ -n "${PG_SHARED_PRELOAD_LIBRARIES}" ]] && postgresql_conf_set "shared_preload_libraries" "${PG_SHARED_PRELOAD_LIBRARIES}" - # Update default value for 'include_dir' directive - # ref: https://github.com/postgres/postgres/commit/fb9c475597c245562a28d1e916b575ac4ec5c19f#diff-f5544d9b6d218cc9677524b454b41c60 - if ! grep include_dir "${PG_CONF_FILE}" > /dev/null; then - postgresql_error "include_dir line is not present in ${PG_CONF_FILE}. This may be due to a changes in a new version of PostgreSQL. Please check" - exit 1 - fi postgresql_conf_set "include_dir" "conf.d" mkdir -p "${APP_CONF_DIR}/conf.d" + + postgresql_configure_from_environment_variables } # 生成初始 pg_hba.conf 配置 diff --git a/customer/usr/local/bin/run.sh b/customer/usr/local/bin/run.sh index bdb6ea8..fcbb526 100755 --- a/customer/usr/local/bin/run.sh +++ b/customer/usr/local/bin/run.sh @@ -14,9 +14,9 @@ set -o pipefail LOG_I "** Processing run.sh **" -flags=("-D" "/srv/data/${APP_NAME}/data") +flags=("--config-file=${PG_CONF_FILE}" "--hba_file=${PG_HBA_FILE}") [[ -z "${APP_EXTRA_FLAGS:-}" ]] || flags=("${flags[@]}" "${APP_EXTRA_FLAGS[@]}") -START_COMMAND=("${APP_EXEC}") +START_COMMAND=("postgres") LOG_I "** Starting ${APP_NAME} **" if is_root; then