diff --git a/10/Dockerfile b/10/Dockerfile index 432ac20..637554e 100644 --- a/10/Dockerfile +++ b/10/Dockerfile @@ -151,4 +151,4 @@ EXPOSE 5432 ENTRYPOINT ["entrypoint.sh"] # 应用程序的服务命令,必须使用非守护进程方式运行。如果使用变量,则该变量必须在运行环境中存在(ENV可以获取) -CMD ["postgres", "--config-file=${PG_CONF_FILE}"] +CMD ["${APP_EXEC}", "--config-file=${PG_CONF_FILE}"] diff --git a/10/prebuilds/usr/local/bin/appcommon.sh b/10/prebuilds/usr/local/bin/appcommon.sh index 52fefb3..002ce33 100644 --- a/10/prebuilds/usr/local/bin/appcommon.sh +++ b/10/prebuilds/usr/local/bin/appcommon.sh @@ -495,11 +495,7 @@ app_start_server_bg() { # -o command line options to pass to postgres or initdb local -r pg_ctl_flags=("-W" "-D" "$PG_DATA_DIR" "-l" "$PG_LOG_FILE" "-o" "--config-file=$PG_CONF_FILE --external_pid_file=$PG_PID_FILE --hba_file=$PG_HBA_FILE") LOG_I "Starting ${APP_NAME} in background..." - local pg_ctl_cmd=() - if _is_run_as_root; then - pg_ctl_cmd+=("gosu" "$APP_USER") - fi - pg_ctl_cmd+=(pg_ctl) + local pg_ctl_cmd=(pg_ctl) if is_boolean_yes "${ENV_DEBUG}"; then "${pg_ctl_cmd[@]}" "start" "${pg_ctl_flags[@]}" else @@ -507,11 +503,7 @@ app_start_server_bg() { fi local -r check_args=("-h" "localhost" "-p" "${PG_PORT_NUMBER}" "-U" "postgres") - local check_cmd=() - if _is_run_as_root; then - check_cmd=("gosu" "$APP_USER") - fi - check_cmd+=(pg_isready) + local check_cmd=(pg_isready) local counter=$PG_INIT_MAX_TIMEOUT LOG_I "Checking ${APP_NAME} ready status..." while ! PGPASSWORD=$PG_REPLICATION_PASSWORD "${check_cmd[@]}" "${check_args[@]}" >/dev/null 2>&1; do @@ -728,11 +720,7 @@ postgresql_master_init_db() { #initdb+=("-o" "--config-file=$PG_CONF_FILE --external_pid_file=$PG_PID_FILE --hba_file=$PG_HBA_FILE") initdb_args+=("--waldir=$APP_DATA_LOG_DIR") - local initdb_cmd=() - if _is_run_as_root; then - initdb_cmd+=("gosu" "$APP_USER") - fi - initdb_cmd+=(initdb) + local initdb_cmd=(initdb) LOG_I "Initializing PostgreSQL database" @@ -757,11 +745,7 @@ postgresql_master_init_db() { postgresql_slave_init_db() { LOG_I "Waiting for replication master to accept connections (${PG_INIT_MAX_TIMEOUT} seconds)..." local -r check_args=("-U" "$PG_REPLICATION_USER" "-h" "$PG_MASTER_HOST" "-p" "$PG_MASTER_PORT_NUMBER" "-d" "postgres") - local check_cmd=() - if _is_run_as_root; then - check_cmd=("gosu" "$APP_USER") - fi - check_cmd+=(pg_isready) + local check_cmd=(pg_isready) local ready_counter=$PG_INIT_MAX_TIMEOUT while ! PGPASSWORD=$PG_REPLICATION_PASSWORD "${check_cmd[@]}" "${check_args[@]}" >/dev/null 2>&1;do @@ -776,11 +760,7 @@ postgresql_slave_init_db() { LOG_I "Replicating the database from node master..." #local -r backup_args=("-D" "$PG_DATA_DIR" -d "hostaddr=$PG_MASTER_HOST port=$PG_MASTER_PORT_NUMBER user=$PG_REPLICATION_USER password=$PG_REPLICATION_PASSWORD" -v -Fp -Xs local -r backup_args=("-D" "$PG_DATA_DIR" "-U" "$PG_REPLICATION_USER" "-h" "$PG_MASTER_HOST" "-p" "$PG_MASTER_PORT_NUMBER" "-X" "stream" "-w" "-v" "-P") - local backup_cmd=() - if _is_run_as_root; then - backup_cmd+=("gosu" "$APP_USER") - fi - backup_cmd+=(pg_basebackup) + local backup_cmd=(pg_basebackup) local replication_counter=$PG_INIT_MAX_TIMEOUT while ! PGPASSWORD=$PG_REPLICATION_PASSWORD "${backup_cmd[@]}" "${backup_args[@]}";do diff --git a/10/prebuilds/usr/local/bin/entrypoint.sh b/10/prebuilds/usr/local/bin/entrypoint.sh index a097363..b7905cf 100644 --- a/10/prebuilds/usr/local/bin/entrypoint.sh +++ b/10/prebuilds/usr/local/bin/entrypoint.sh @@ -54,6 +54,9 @@ docker_ensure_dir_and_configs() { } _main() { + # 替换命令行中的变量 + set -- $(eval echo "$@") + # 如果命令行参数是以配置参数("-")开始,修改执行命令,确保使用可执行应用命令启动服务器 if [ "${1:0:1}" = '-' ]; then set -- "${APP_EXEC}" "$@" @@ -87,9 +90,11 @@ _main() { LOG_D "Change permissions of stdout/stderr to 0622" chmod 0622 /dev/stdout /dev/stderr + LOG_I "" LOG_I "Restart container with default user: ${APP_USER}" + LOG_I " command: $@" export RESTART_FLAG=1 - exec gosu "${APP_USER}" "$0" $(eval echo "$@") + exec gosu "${APP_USER}" "$0" "$@" fi # 执行预初始化操作 @@ -103,8 +108,8 @@ _main() { fi LOG_I "Start container with command: $@" - # 执行命令行。使用 evel 替换后,可支持 Dockerfile 脚本的 CMD 命令中使用变量 - exec $(eval echo "$@") + # 执行命令行。 + exec "$@" } # 脚本入口命令 diff --git a/10/prebuilds/usr/local/scripts/libcommon.sh b/10/prebuilds/usr/local/scripts/libcommon.sh index 01da0a6..0175ac3 100644 --- a/10/prebuilds/usr/local/scripts/libcommon.sh +++ b/10/prebuilds/usr/local/scripts/libcommon.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Ver: 1.0 by Endial Fang (endial@126.com) +# Ver: 1.1 by Endial Fang (endial@126.com) # # shellcheck disable=SC1091 @@ -122,10 +122,10 @@ ensure_config_file_exist() { # 布尔值 _is_run_as_root() { if [[ "$(id -u)" = "0" ]]; then - LOG_D "Run as root" + LOG_D "Check if run as root: Yes" true else - LOG_D "User id: $(id -u)" + LOG_D "Check if run as root: No (ID $(id -u))" false fi } diff --git a/10/prebuilds/usr/local/scripts/libfs.sh b/10/prebuilds/usr/local/scripts/libfs.sh index 46268ac..f9f73d3 100644 --- a/10/prebuilds/usr/local/scripts/libfs.sh +++ b/10/prebuilds/usr/local/scripts/libfs.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Ver: 1.0 by Endial Fang (endial@126.com) +# Ver: 1.1 by Endial Fang (endial@126.com) # # 文件管理函数库