|
1 | | - |
2 | 1 | ARG PG_MAJOR=17 |
| 2 | +ARG PREV_PG_MAJOR=15 |
3 | 3 | ARG TIMESCALE_VERSION=2.22 |
4 | 4 |
|
5 | | -FROM timescale/timescaledb-ha:pg17-ts${TIMESCALE_VERSION} AS trimmed |
6 | | -LABEL maintainer="support@openremote.io" |
| 5 | +# Stage 1: Get PostgreSQL ${PREV_PG_MAJOR} binaries for upgrade support |
| 6 | +FROM timescale/timescaledb-ha:pg${PG_MAJOR}-ts${TIMESCALE_VERSION}-all AS pg-all |
7 | 7 |
|
8 | 8 | USER root |
9 | 9 |
|
10 | | -# install fd to find files to speed up chown and chgrp |
11 | | -RUN apt-get update && apt-get install -y fd-find && rm -rf /var/lib/apt/lists/* |
12 | | - |
13 | | -# Give postgres user the same UID and GID as the old alpine postgres image to simplify migration of existing DB |
14 | | -RUN usermod -u 70 postgres \ |
15 | | - && groupmod -g 70 postgres \ |
16 | | - && (fd / -group 1000 -exec chgrp -h postgres {} \; || true) \ |
17 | | - && (fd / -user 1000 -exec chown -h postgres {} \; || true) |
| 10 | +ARG PREV_PG_MAJOR |
18 | 11 |
|
19 | | -# Set PGDATA to the same location as our old alpine image |
20 | | -RUN mkdir -p /var/lib/postgresql && mv /home/postgres/pgdata/* /var/lib/postgresql/ && chown -R postgres:postgres /var/lib/postgresql |
| 12 | +# Strip debug symbols and remove unnecessary files from PG ${PREV_PG_MAJOR} in this stage |
| 13 | +# For pg_upgrade we need bin/, lib/, and extension/ (for TimescaleDB upgrade scripts) |
| 14 | +RUN find /usr/lib/postgresql/${PREV_PG_MAJOR} -type f -name '*.so*' -exec strip --strip-unneeded {} \; 2>/dev/null || true \ |
| 15 | + && find /usr/lib/postgresql/${PREV_PG_MAJOR} -type f -executable -exec strip --strip-unneeded {} \; 2>/dev/null || true \ |
| 16 | + && rm -rf /usr/share/postgresql/${PREV_PG_MAJOR}/man \ |
| 17 | + /usr/share/postgresql/${PREV_PG_MAJOR}/doc \ |
| 18 | + /usr/share/postgresql/${PREV_PG_MAJOR}/contrib |
21 | 19 |
|
22 | | -# Add custom entry point (see file header for details) |
23 | | -COPY or-entrypoint.sh / |
24 | | -RUN chmod +x /or-entrypoint.sh |
| 20 | +# Stage 2: Prepare the main image with UID/GID changes and cleanup |
| 21 | +FROM timescale/timescaledb-ha:pg${PG_MAJOR}-ts${TIMESCALE_VERSION} AS final |
| 22 | +LABEL maintainer="support@openremote.io" |
25 | 23 |
|
26 | | -# Add custom initdb script(s) |
27 | | -COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/ |
28 | | -RUN chmod +x /docker-entrypoint-initdb.d/* |
| 24 | +USER root |
29 | 25 |
|
| 26 | +ARG PREV_PG_MAJOR |
30 | 27 |
|
31 | | -# Below is mostly copied from https://github.com/timescale/timescaledb-docker-ha/blob/master/Dockerfile (with OR specific entrypoint, |
32 | | -# workdir and OR env defaults) |
| 28 | +# Copy PG ${PREV_PG_MAJOR} bin and lib directories for pg_upgrade |
| 29 | +COPY --from=pg-all /usr/lib/postgresql/${PREV_PG_MAJOR}/bin /usr/lib/postgresql/${PREV_PG_MAJOR}/bin |
| 30 | +COPY --from=pg-all /usr/lib/postgresql/${PREV_PG_MAJOR}/lib /usr/lib/postgresql/${PREV_PG_MAJOR}/lib |
| 31 | +# Copy share files including extensions (needed for TimescaleDB upgrade on old PG before pg_upgrade) |
| 32 | +COPY --from=pg-all /usr/share/postgresql/${PREV_PG_MAJOR} /usr/share/postgresql/${PREV_PG_MAJOR} |
33 | 33 |
|
34 | | -# Get the -all variant which contains multiple PostgreSQL versions |
35 | | -# According to TimescaleDB docs: "timescale/timescaledb-ha images have the files necessary to run previous versions" |
36 | | -FROM timescale/timescaledb-ha:pg17-ts${TIMESCALE_VERSION}-all AS trimmed-all |
| 34 | +# Copy entrypoint scripts |
| 35 | +COPY or-entrypoint.sh / |
| 36 | +COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/ |
37 | 37 |
|
38 | | -## Create a smaller Docker image from the builder image |
39 | | -FROM scratch |
40 | | -COPY --from=trimmed / / |
| 38 | +# Install fd-find, fix UID/GID, setup directories, strip binaries, and cleanup - all in one layer |
| 39 | +RUN apt-get update && apt-get install -y --no-install-recommends fd-find \ |
| 40 | + # Give postgres user the same UID and GID as the old alpine postgres image |
| 41 | + && usermod -u 70 postgres \ |
| 42 | + && groupmod -g 70 postgres \ |
| 43 | + && (fdfind . / -group 1000 -exec chgrp -h postgres {} \; 2>/dev/null || true) \ |
| 44 | + && (fdfind . / -user 1000 -exec chown -h postgres {} \; 2>/dev/null || true) \ |
| 45 | + # Set PGDATA to the same location as our old alpine image |
| 46 | + && mkdir -p /var/lib/postgresql \ |
| 47 | + && mv /home/postgres/pgdata/* /var/lib/postgresql/ \ |
| 48 | + && chown -R postgres:postgres /var/lib/postgresql \ |
| 49 | + # Make scripts executable |
| 50 | + && chmod +x /or-entrypoint.sh /docker-entrypoint-initdb.d/* \ |
| 51 | + # Strip debug symbols from PostgreSQL binaries to reduce size |
| 52 | + && find /usr/lib/postgresql -type f -name '*.so*' -exec strip --strip-unneeded {} \; 2>/dev/null || true \ |
| 53 | + && find /usr/lib/postgresql -type f -executable -exec strip --strip-unneeded {} \; 2>/dev/null || true \ |
| 54 | + # Remove fd-find and clean up |
| 55 | + && apt-get purge -y fd-find \ |
| 56 | + && apt-get autoremove -y --purge \ |
| 57 | + && apt-get clean \ |
| 58 | + && rm -rf /var/lib/apt/lists/* \ |
| 59 | + /var/cache/apt/* \ |
| 60 | + /var/log/* \ |
| 61 | + /usr/share/doc/* \ |
| 62 | + /usr/share/man/* \ |
| 63 | + /usr/share/info/* \ |
| 64 | + /usr/share/lintian/* \ |
| 65 | + /usr/share/locale/* \ |
| 66 | + /tmp/* \ |
| 67 | + /var/tmp/* \ |
| 68 | + /root/.cache \ |
| 69 | + /home/postgres/.cache \ |
| 70 | + /usr/local/lib/pgai \ |
| 71 | + /usr/share/postgresql/*/man \ |
| 72 | + /usr/share/postgresql/*/doc |
41 | 73 |
|
42 | 74 | ARG PG_MAJOR |
43 | | - |
44 | | -## Copy only PostgreSQL 14 and 15 for upgrade support |
45 | | -COPY --from=trimmed-all /usr/lib/postgresql/14 /usr/lib/postgresql/14 |
46 | | -COPY --from=trimmed-all /usr/lib/postgresql/15 /usr/lib/postgresql/15 |
47 | | -COPY --from=trimmed-all /usr/share/postgresql/14 /usr/share/postgresql/14 |
48 | | -COPY --from=trimmed-all /usr/share/postgresql/15 /usr/share/postgresql/15 |
| 75 | +ARG PREV_PG_MAJOR |
49 | 76 |
|
50 | 77 | # Increment this to indicate that a re-index should be carried out on first startup with existing data; REINDEX can still be overidden |
51 | 78 | # with OR_DISABLE_REINDEX=true |
@@ -80,6 +107,7 @@ ENV PGROOT=/var/lib/postgresql \ |
80 | 107 | POSTGRES_USER=${POSTGRES_USER:-postgres} \ |
81 | 108 | POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres} \ |
82 | 109 | PG_MAJOR=$PG_MAJOR \ |
| 110 | + PREV_PG_MAJOR=$PREV_PG_MAJOR \ |
83 | 111 | OR_REINDEX_COUNTER=${OR_REINDEX_COUNTER} \ |
84 | 112 | OR_DISABLE_REINDEX=${OR_DISABLE_REINDEX:-false} \ |
85 | 113 | POSTGRES_MAX_CONNECTIONS=${POSTGRES_MAX_CONNECTIONS:-50} \ |
|
0 commit comments