Skip to content

Instantly share code, notes, and snippets.

@haapmik
Last active June 26, 2024 18:44
Show Gist options
  • Save haapmik/e0b9517cf2e1c9ac70c04b09f3d7fd96 to your computer and use it in GitHub Desktop.
Save haapmik/e0b9517cf2e1c9ac70c04b09f3d7fd96 to your computer and use it in GitHub Desktop.
An example Dockerfile for PostgreSQL that uses Alpine as the base image. It employs a multi-stage build to re-build gosu with a newer Go version and also includes additional PostgreSQL extensions.
# Common arguments
ARG PG_EXTENSION_DIR=/usr/local/share/postgresql/extension
ARG PG_LIB_DIR=/usr/local/lib/postgresql
ARG GO_BIN_PATH=/go/bin
# STAGE: build 'gosu' with latest golang version
# This will get rid of all false positive CVE alerts related to golang 1.18,
# see https://github.com/tianon/gosu/security
FROM library/golang:alpine3.20 AS build-gosu
RUN go install github.com/tianon/gosu@latest
# STAGE: base image for postgresql
FROM library/postgres:15-alpine3.20 AS base-postgres-image
ARG GO_BIN_PATH
COPY --from=build-gosu $GO_BIN_PATH/gosu /usr/local/bin/gosu
# STAGE: build 'pgsodium' extension
FROM base-postgres-image AS build-pgsodium
RUN apk add \
--no-cache \
git make libc-dev clang15 llvm15-dev libsodium-dev
RUN mkdir -p /src \
&& git clone https://github.com/michelp/pgsodium.git /src/pgsodium \
&& cd /src/pgsodium \
&& make install
# STAGE: final image
FROM base-postgres-image AS final-image
ARG PG_EXTENSION_DIR
ARG PG_LIB_DIR
COPY --from=build-pgsodium $PG_EXTENSION_DIR $PG_EXTENSION_DIR
COPY --from=build-pgsodium $PG_LIB_DIR $PG_LIB_DIR
RUN apk add \
--no-cache \
libsodium
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment