Skip to content

Commit

Permalink
Merge pull request #1 from factisresearch/pb/safeinit
Browse files Browse the repository at this point in the history
Make sure we don't start up with an incompletely initialized database
  • Loading branch information
bachorp authored Jun 12, 2023
2 parents 14145a3 + f091f82 commit 2d70086
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ RUN wget -q -O - https://github.com/pgbackrest/pgbackrest/archive/release/2.45.t

RUN cd /build/pgbackrest-release-2.45/src && ./configure && make

FROM postgres:15-bullseye as patch

# https://github.com/docker-library/postgres/issues/159
RUN apt-get update
RUN apt-get install -y \
patch

COPY safe-initdb.docker-entrypoint.sh.diff .
RUN patch /usr/local/bin/docker-entrypoint.sh < safe-initdb.docker-entrypoint.sh.diff

FROM postgres:15-bullseye as install

# https://pgbackrest.org/user-guide.html#installation
Expand Down Expand Up @@ -65,6 +75,8 @@ COPY pgbackrest-init.sh /docker-entrypoint-initdb.d/
COPY pg_cron.sql /docker-entrypoint-initdb.d/
COPY plpython3u.sql /docker-entrypoint-initdb.d/

COPY --from=patch /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

FROM setup as run

CMD ["postgres", "--config_file=/etc/postgresql/postgresql.conf"]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.1.0
29 changes: 29 additions & 0 deletions safe-initdb.docker-entrypoint.sh.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@@ -308,8 +308,19 @@ _main() {
exec gosu postgres "$BASH_SOURCE" "$@"
fi

+ local initFailed="$PGDATA/INITFAILED"
+
+ if [ -s "$initFailed" ]; then
+ cat >&2 <<-'EOE'
+ Error: Previous database initialization failed.
+ EOE
+ exit 1
+ fi
+
# only run initialization on an empty data directory
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
+ trap "mkdir $initFailed" EXIT
+
docker_verify_minimum_env

# check dir permissions to reduce likelihood of half-initialized database
@@ -329,6 +340,8 @@ _main() {
docker_temp_server_stop
unset PGPASSWORD

+ trap - EXIT
+
cat <<-'EOM'

PostgreSQL init process complete; ready for start up.

0 comments on commit 2d70086

Please sign in to comment.