|
| 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 | + |
| 7 | +USER 0 |
| 8 | + |
| 9 | +RUN set -eux && \ |
| 10 | + # Initial system libraries |
| 11 | + ldconfig -p | awk '{print $NF}' | grep '^/' | sort | uniq > /tmp/base-image-libs.out && \ |
| 12 | + # Install tds-fdw |
| 13 | + apt-get update && \ |
| 14 | + apt-get install -y --no-install-recommends \ |
| 15 | + "postgresql-${PG_MAJOR}-tds-fdw=${EXT_VERSION}" |
| 16 | + |
| 17 | +# Gather tds-fdw system libraries and their licenses |
| 18 | +RUN mkdir -p /system /licenses && \ |
| 19 | + # Get libraries |
| 20 | + ldd /usr/lib/postgresql/${PG_MAJOR}/lib/tds-fdw*.so \ |
| 21 | + | awk '{print $3}' | grep '^/' | sort | uniq > /tmp/all-deps.out && \ |
| 22 | + # Extract all the libs that aren't already part of the base image |
| 23 | + comm -13 /tmp/base-image-libs.out /tmp/all-deps.out > /tmp/libraries.out && \ |
| 24 | + while read -r lib; do \ |
| 25 | + resolved=$(readlink -f "$lib"); \ |
| 26 | + dir=$(dirname "$lib"); \ |
| 27 | + base=$(basename "$lib"); \ |
| 28 | + # Copy the real file |
| 29 | + cp -a "$resolved" /system/; \ |
| 30 | + # Reconstruct all its symlinks |
| 31 | + for file in "$dir"/"${base%.so*}.so"*; do \ |
| 32 | + [ -e "$file" ] || continue; \ |
| 33 | + # If it's a symlink and it resolves to the same real file, we reconstruct it |
| 34 | + if [ -L "$file" ] && [ "$(readlink -f "$file")" = "$resolved" ]; then \ |
| 35 | + ln -sf "$(basename "$resolved")" "/system/$(basename "$file")"; \ |
| 36 | + fi; \ |
| 37 | + done; \ |
| 38 | + done < /tmp/libraries.out && \ |
| 39 | + # Get licenses |
| 40 | + for lib in $(find /system -maxdepth 1 -type f -name '*.so*'); do \ |
| 41 | + # Get the name of the pkg that installed the library |
| 42 | + pkg=$(dpkg -S "$(basename "$lib")" | grep -v "diversion by" | awk -F: '/:/{print $1; exit}'); \ |
| 43 | + [ -z "$pkg" ] && continue; \ |
| 44 | + mkdir -p "/licenses/$pkg" && cp -a "/usr/share/doc/$pkg/copyright" "/licenses/$pkg/copyright"; \ |
| 45 | + done |
| 46 | + |
| 47 | + |
| 48 | +FROM scratch |
| 49 | +ARG PG_MAJOR |
| 50 | + |
| 51 | +# Licenses |
| 52 | +COPY --from=builder /licenses /licenses/ |
| 53 | +COPY --from=builder \ |
| 54 | + /usr/share/doc/postgresql-${PG_MAJOR}-tds-fdw/copyright \ |
| 55 | + /licenses/postgresql-${PG_MAJOR}-tds-fdw/ |
| 56 | + |
| 57 | +# Libraries |
| 58 | +COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/tds_fdw* /lib/ |
| 59 | + |
| 60 | +COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/bitcode/ /lib/bitcode/ |
| 61 | + |
| 62 | +# Share |
| 63 | +COPY --from=builder \ |
| 64 | + /usr/share/postgresql/${PG_MAJOR}/extension/tds_fdw* \ |
| 65 | + /share/extension/ |
| 66 | + |
| 67 | +# System libs |
| 68 | +COPY --from=builder /system /system/ |
| 69 | + |
| 70 | +USER 65532:65532 |
0 commit comments