|
| 1 | +ARG BASE=ghcr.io/cloudnative-pg/postgresql:18-minimal-trixie |
| 2 | +FROM $BASE AS builder |
| 3 | + |
| 4 | +ARG PG_MAJOR |
| 5 | +ARG EXT_VERSION |
| 6 | +ARG EXT_MAJOR |
| 7 | + |
| 8 | +USER 0 |
| 9 | + |
| 10 | +RUN set -eux && \ |
| 11 | + # Initial system libraries |
| 12 | + ldconfig -p | awk '{print $NF}' | grep '^/' | sort | uniq > /tmp/base-image-libs.out && \ |
| 13 | + # Install PostGIS |
| 14 | + apt-get update && \ |
| 15 | + apt-get install -y --no-install-recommends "postgresql-${PG_MAJOR}-postgis-${EXT_MAJOR}=${EXT_VERSION}" |
| 16 | + |
| 17 | +# Gather PostGIS system libraries and their licenses |
| 18 | +RUN mkdir -p /system /licenses && \ |
| 19 | + # Get libraries |
| 20 | + ldd /usr/lib/postgresql/${PG_MAJOR}/lib/address_standardizer*.so \ |
| 21 | + /usr/lib/postgresql/${PG_MAJOR}/lib/postgis*.so \ |
| 22 | + | awk '{print $3}' | grep '^/' | sort | uniq > /tmp/all-deps.out && \ |
| 23 | + # Extract all the libs that aren't already part of the base image |
| 24 | + comm -13 /tmp/base-image-libs.out /tmp/all-deps.out > /tmp/libraries.out && \ |
| 25 | + while read -r lib; do \ |
| 26 | + # Resolve the symlink to get the real file |
| 27 | + resolved=$(readlink -f "$lib"); \ |
| 28 | + # Base name of real file |
| 29 | + basename=$(basename "$resolved"); \ |
| 30 | + # Copy the real file and all its symlinks |
| 31 | + cp -a "$(dirname "$resolved")/${basename%.so*}.so"* /system/; \ |
| 32 | + done < /tmp/libraries.out && \ |
| 33 | + # Get licenses |
| 34 | + for lib in $(find /system -maxdepth 1 -type f -name '*.so*'); do \ |
| 35 | + # Get the name of the pkg that installed the library |
| 36 | + pkg=$(dpkg -S "$(basename "$lib")" | awk -F: '/:/{print $1; exit}'); \ |
| 37 | + [ -z "$pkg" ] && continue; \ |
| 38 | + mkdir -p "/licenses/$pkg" && cp -a "/usr/share/doc/$pkg/copyright" "/licenses/$pkg/copyright"; \ |
| 39 | + done |
| 40 | + |
| 41 | + |
| 42 | +FROM scratch |
| 43 | +ARG PG_MAJOR |
| 44 | +ARG EXT_MAJOR |
| 45 | + |
| 46 | +# Licenses |
| 47 | +COPY --from=builder /licenses /licenses/ |
| 48 | +COPY --from=builder /usr/share/doc/postgresql-${PG_MAJOR}-postgis-${EXT_MAJOR}/copyright /licenses/postgresql-${PG_MAJOR}-postgis-${EXT_MAJOR}/ |
| 49 | + |
| 50 | +# Libraries |
| 51 | +COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/address_standardizer* /usr/lib/postgresql/${PG_MAJOR}/lib/postgis* /lib/ |
| 52 | +COPY --from=builder /usr/lib/postgresql/18/lib/bitcode/ /lib/bitcode/ |
| 53 | + |
| 54 | +# Share |
| 55 | +COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/address_standardizer* /usr/share/postgresql/${PG_MAJOR}/extension/postgis* /share/extension/ |
| 56 | +COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/contrib/postgis* /share/contrib/ |
| 57 | + |
| 58 | +# System libs |
| 59 | +COPY --from=builder /system /system/ |
| 60 | + |
| 61 | +USER 65532:65532 |
0 commit comments