Skip to content

Commit

Permalink
Robustness improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner committed Feb 22, 2024
1 parent 709d7c9 commit 6888547
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ COPY --from=trimmed-all /usr/share/postgresql/${PG_MAJOR_PREVIOUS} /usr/share/po
# with OR_DISABLE_REINDEX=true
ARG OR_REINDEX_COUNTER=1

ENTRYPOINT ["/bin/sh", "-c", "/or-entrypoint.sh postgres -c max_connections=${POSTGRES_MAX_CONNECTIONS}"]
#postgres -c "max_connections=${POSTGRES_MAX_CONNECTIONS}"
# This is important otherwise connections will prevent a graceful shutdown
STOPSIGNAL SIGINT

#ENTRYPOINT ["/bin/sh", "-c", "/or-entrypoint.sh postgres -c max_connections=${POSTGRES_MAX_CONNECTIONS}"]
ENTRYPOINT ["/or-entrypoint.sh"]

# Use exec form of CMD with exec call so kill signals are correctly forwarded whilst allowing variable expansion
# see: https://github.com/moby/moby/issues/5509#issuecomment-890126570
CMD ["postgres"]

ENV PGROOT=/var/lib/postgresql \
PGDATA=/var/lib/postgresql/data \
Expand Down
44 changes: 44 additions & 0 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -----------------------------------------------------------------------------------------------
# POSTGIS and TimescaleDB (inc. toolkit for hyperfunctions) image built for aarch64 support
# using alpine base image.
#
# timescale/timescaledb-ha image is ubuntu based and only currently supports amd64; they are
# working on ARM64 support in timescaledev/timescaledb-ha see:
#
# https://github.com/timescale/timescaledb-docker-ha/pull/355
#
# See this issue for POSTGIS base image aarch64 support discussion:
#
# https://github.com/postgis/docker-postgis/issues/216
# ------- ----------------------------------------------------------------------------------------

# We get POSTGIS and timescale+toolkit from this image
FROM timescaledev/timescaledb-ha:pg15-multi as timescale-ha


# This base image is alpine based - timescale toolkit requires glibc 2.3+ so we install it into alpine image
# This still doesn't work as timescale code is compiled against glibc and some references don't match with gcompat

FROM timescale/timescaledb:latest-pg15
MAINTAINER support@openremote.io

ENV GLIBC_VERSION 2.35-r0
ENV TZ ${TZ:-Europe/Amsterdam}
ENV PGTZ ${PGTZ:-Europe/Amsterdam}
ENV POSTGRES_DB ${POSTGRES_DB:-openremote}
ENV POSTGRES_USER ${POSTGRES_USER:-postgres}
ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD:-postgres}

# Add glibc
RUN apk add gcompat


COPY --from=timescale-ha /usr/lib/postgresql/15/lib/bitcode/postgis-3/ /usr/local/lib/postgresql/bitcode/
COPY --from=timescale-ha /usr/lib/postgresql/15/lib/postgis* /usr/local/lib/postgresql/
COPY --from=timescale-ha /docker-entrypoint-initdb.d/010_install_timescaledb_toolkit.sh /docker-entrypoint-initdb.d/010_install_timescaledb_toolkit.sh
COPY --from=timescale-ha /usr/lib/postgresql/15/lib/timescaledb* /usr/local/lib/postgresql/
COPY --from=timescale-ha /usr/bin/timescale* /usr/local/bin/
COPY --from=timescale-ha /usr/share/postgresql/15/extension/postgis* /usr/local/share/postgresql/extension/
COPY --from=timescale-ha /usr/share/postgresql/15/extension/timescale* /usr/local/share/postgresql/extension/

HEALTHCHECK --interval=3s --timeout=3s --start-period=2s --retries=30 CMD pg_isready
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
POSTGIS and TimescaleDB (inc. toolkit for hyperfunctions) image built for aarch64 support using `timescaledev/timescaledb-ha` base image with:

- OR specific ENV variables and a healthcheck added
- Easy configuration of `max_connections` using `POSTGRES_MAX_CONNECTIONS` environment variable (set to `-1` to disable this setting)
- PGDATA path set to match old Alpine image (for ease of DB migration)
- POSTGRES user UID and GID changed to match old Alpine image (for ease of DB migration)
- Auto upgrade of database with PG major version changes from previous PG major version; can be disabled using
Expand Down
29 changes: 16 additions & 13 deletions or-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
source /docker-entrypoint.sh
docker_setup_env

# Append max connections arg if needed
if [ $POSTGRES_MAX_CONNECTIONS -gt 0 ]; then
set -- "$@" -c max_connections=${POSTGRES_MAX_CONNECTIONS}
fi

# Check for presence of old/new directories, indicating a failed previous autoupgrade
echo "----------------------------------------------------------------------"
Expand All @@ -30,26 +34,15 @@ echo "--------------------------------------------------------------------------
echo "No artifacts found from a failed previous autoupgrade. Continuing the process."
echo "-------------------------------------------------------------------------------"

echo "DB STATUS: "
pg_ctl -D "$PGDATA" status

if [ -n "$DATABASE_ALREADY_EXISTS" ]; then

echo "-----------------------------------------"
echo "Performing checks on existing database..."
echo "-----------------------------------------"

# Just make sure postgres isn't still running for some reason
set +e
pg_ctl -D "$PGDATA" -m immedate stop
set -e

# Remove any stale PID file
rm -f "${PGDATA}/postmaster.pid"

# Make sure timescaledb library is set to preload (won't work otherwise)
echo "---------------------------------------------------------------------------------------"
echo "Existing postgresql.conf found checking for shared_preload_libraries = 'timescaledb'..."
echo "Existing postgresql.conf found checking for shared_preload_libraries = 'timescaledb'..."
echo "---------------------------------------------------------------------------------------"
RESULT=$(cat "$PGDATA/postgresql.conf" | grep "^shared_preload_libraries = 'timescaledb'" || true)

Expand Down Expand Up @@ -88,8 +81,18 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
echo "Postgres major version is newer than the existing DB, performing auto upgrade..."
echo "---------------------------------------------------------------------------------"

if [ -f "${PGDATA}/postmaster.pid" ]; then
echo "-----------------------------------------------------------------------------------------------------"
echo "Looks like the server did not previously shutdown properly which will prevent pg_upgrade from working"
echo "try stopping the whole stack, bringing only the postgresql container up and then stopping it again"
echo "-----------------------------------------------------------------------------------------------------"
exit 1
fi

if [ ! -d "/usr/lib/postgresql/${DB_VERSION}" ]; then
echo "--------------------------------------------------------------------------------------------------"
echo "Postgres executable version '$DB_VERSION' is not included in this image so cannot auto upgrade"
echo "--------------------------------------------------------------------------------------------------"
exit 1
fi

Expand Down Expand Up @@ -194,7 +197,7 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
echo "Copying reindex and TS version files across"
echo "--------------------------------------------------------------"
cp -f ${OLD}/OR_REINDEX_* ${PGDATA}
cp -f ${OLD}/TS_VERSION ${PGDATA}
cp -f ${OLD}/OR_TS_VERSION ${PGDATA}
echo "-------------------------------------------------------------------"
echo "Copying reindex files is complete"
echo "-------------------------------------------------------------------"
Expand Down

0 comments on commit 6888547

Please sign in to comment.