fboulnois / pg_uuidv7

A tiny Postgres extension to create version 7 UUIDs
Mozilla Public License 2.0
298 stars 25 forks source link

[Docs] Add Docker COPY from Example #31

Open yordis opened 6 months ago

yordis commented 6 months ago

Hey there, I wonder if we could "merge" the pg_uuidv7 image into the base image.

I have other extension requirements aside from pg_uuidv7, and I am hoping that such a pattern becomes familiar enough so Extension Authors could provide a straightforward image to COPY from instead of relying on tools like pgxn or pgxman, or deal with "complex" (unfamiliar) installations steps and instead take advantage of Official Docker Image.

FROM postgres:latest

COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0 /[help here] /[help here]

I appreciate any help you can provide.

yordis commented 6 months ago

Ideally, create a ghcr.io/fboulnois/pg_uuidv7:1.5.0-extension image so that it is safe to copy everything from it:

FROM postgres:latest

COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0-extension /PGEXTENSION/${PG_MAJOR} /

Where that tree under /PGEXTENSION has ONLY the files required to copy into my image

heyztb commented 6 months ago

Edit: maybe it's not exactly what was asked for, leaving it here anyways. if you need this alongside other extensions, it might not be the most ergonomic way of doing it, but it does work.

Hey there, while I'm not the maintainer for this extension, I did see this and figured this could be done pretty easily.

Here's an example I put together

FROM postgres:alpine3.18@sha256:354a818d8a1e94707704902edb8c4e98b0eb64de3ee0354c4d94b4e2905c63ee

RUN apk update && apk add curl \
    && cd $(mktemp -d) \
    && curl -LO "https://github.com/fboulnois/pg_uuidv7/releases/download/v1.5.0/pg_uuidv7.tar.gz" \
    && curl -LO "https://github.com/fboulnois/pg_uuidv7/releases/download/v1.5.0/SHA256SUMS" \
    && tar xf pg_uuidv7.tar.gz \
    && sha256sum -c SHA256SUMS \
    && if [ $? -ne 0 ]; then echo "Checksum verification failed" && exit 1; fi \
    && cp "$PG_MAJOR/pg_uuidv7.so" "$(pg_config --pkglibdir)" \
    && cp pg_uuidv7--1.5.sql pg_uuidv7.control "$(pg_config --sharedir)/extension"

RUN echo "CREATE EXTENSION pg_uuidv7;" > /docker-entrypoint-initdb.d/create_pg_uuidv7.sql
db=# select uuid_generate_v7();
           uuid_generate_v7           
--------------------------------------
 018e68a2-cab5-79d6-8f57-f7c0e7694dc4
(1 row)

I'm using this alongside Docker Compose, so I've got the environment variables that Postgres needs during setup defined there instead of in this Dockerfile. That looks like this:

services:
  db:
    build: 
      context: .
      dockerfile: pg-Dockerfile
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
fboulnois commented 6 months ago

Here are two other ways to accomplish this directly using the ghcr.io image.

v1:

FROM postgres:16

COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0 /srv/* /srv/

RUN cd /srv \
    && tar xf pg_uuidv7.tar.gz \
    && sha256sum -c SHA256SUMS \
    && cp /srv/${PG_MAJOR}/pg_uuidv7.so /usr/lib/postgresql/${PG_MAJOR}/lib \
    && cp /srv/pg_uuidv7.control /usr/share/postgresql/${PG_MAJOR}/extension \
    && cp /srv/pg_uuidv7--1.5.sql /usr/share/postgresql/${PG_MAJOR}/extension

v2:

FROM postgres:16

COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0 /usr/lib/postgresql/${PG_MAJOR}/lib/pg_uuidv7.so /usr/lib/postgresql/${PG_MAJOR}/lib
COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0 /usr/share/postgresql/${PG_MAJOR}/extension/pg_uuidv7.control /usr/share/postgresql/${PG_MAJOR}/extension
COPY --from=ghcr.io/fboulnois/pg_uuidv7:1.5.0 /usr/share/postgresql/${PG_MAJOR}/extension/pg_uuidv7--1.5.sql /usr/share/postgresql/${PG_MAJOR}/extension
yordis commented 6 months ago

@fboulnois I am doing the V2, but I wish I didn't have to know all file names (for example, 1.5.sql may change in the future) and there was a "safe" way to copy everything into my existing container. Therefore, the files under a given directory must be clean and only include the extension-required code; otherwise, the container that was built may unintentionally override things.

I know I am asking for extra work on your end (I can try to contribute with your support to the feature), but honestly, the ecosystem would benefit from this. I am just learning about so many tools to manage PG Extensions that barely take advantage of Docker.

yordis commented 6 months ago

I made a PR showcasing more and less what I mean. Hopefully, I set the CI correctly!