Skip to content

Commit

Permalink
Add Dockerfile with build args. (#7)
Browse files Browse the repository at this point in the history
* Add Dockerfile with build args.

1. Dockerfile with build args and alpine image
2. update Readme.md
3. Github workflows now verify the docker built images are correct.

---------

Co-authored-by: Jimmy Huang <[email protected]>
  • Loading branch information
jimmy-shaojun and huangjimmy authored Oct 29, 2023
1 parent a627574 commit 2f1a721
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 50 deletions.
128 changes: 123 additions & 5 deletions .github/workflows/postgress12_16.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build pg_cjk_parser for postgres 12 and 16
name: Build and verify pg_cjk_parser for

on:
push:
Expand All @@ -7,7 +7,7 @@ on:
branches: [ "master" ]

jobs:
build_pg12:
PostgreSQL-11:
runs-on: ubuntu-latest
services:
registry:
Expand All @@ -29,13 +29,130 @@ jobs:
uses: docker/build-push-action@v5
with:
push: true
file: Dockerfile_pg12
file: Dockerfile_pg11
tags: localhost:5000/postgres:11-dev
-
name: Run bash script to verify image postgres:11-dev
run: docker pull localhost:5000/postgres:11-dev && docker tag localhost:5000/postgres:11-dev postgres:11-dev && chmod +x ./postgres-11.sh && ./postgres-11.sh

PostgreSQL-12:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
-
name: Build and push
uses: docker/build-push-action@v5
with:
push: true
build-args: POSTGRES_VERSION=12
file: Dockerfile_alpine

tags: localhost:5000/postgres:12-dev
-
name: Run bash script to verify image postgres:12-dev
run: docker pull localhost:5000/postgres:12-dev && docker tag localhost:5000/postgres:12-dev postgres:12-dev && chmod +x ./postgres-12.sh && ./postgres-12.sh

build_pg16:
PostgreSQL-13:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
-
name: Build and push
uses: docker/build-push-action@v5
with:
push: true
build-args: POSTGRES_VERSION=13
file: Dockerfile_alpine
tags: localhost:5000/postgres:dev
-
name: Run bash script to verify image postgres:dev
run: docker pull localhost:5000/postgres:dev && docker tag localhost:5000/postgres:dev postgres:12-dev && chmod +x ./postgres-12.sh && ./postgres-12.sh

PostgreSQL-14:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
-
name: Build and push
uses: docker/build-push-action@v5
with:
push: true
build-args: POSTGRES_VERSION=14
file: Dockerfile_alpine
tags: localhost:5000/postgres:dev
-
name: Run bash script to verify image postgres:dev
run: docker pull localhost:5000/postgres:dev && docker tag localhost:5000/postgres:dev postgres:dev && chmod +x ./postgres-1x.sh && ./postgres-1x.sh

PostgreSQL-15:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
-
name: Build and push
uses: docker/build-push-action@v5
with:
push: true
build-args: POSTGRES_VERSION=15
file: Dockerfile_alpine
tags: localhost:5000/postgres:dev
-
name: Run bash script to verify image postgres:dev
run: docker pull localhost:5000/postgres:dev && docker tag localhost:5000/postgres:dev postgres:dev && chmod +x ./postgres-1x.sh && ./postgres-1x.sh

PostgreSQL-16:
runs-on: ubuntu-latest
services:
registry:
Expand All @@ -57,7 +174,8 @@ jobs:
uses: docker/build-push-action@v5
with:
push: true
file: Dockerfile_pg16
build-args: POSTGRES_VERSION=16
file: Dockerfile_alpine
tags: localhost:5000/postgres:16-dev
-
name: Run bash script to verify image postgres:16-dev
Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ARG POSTGRES_VERSION=16
FROM postgres:$POSTGRES_VERSION as build
ARG POSTGRES_VERSION=16
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-server-dev-$POSTGRES_VERSION gcc make icu-devtools libicu-dev

RUN mkdir -p /root/parser
WORKDIR /root/parser
COPY pg_cjk_parser.c /root/parser/
COPY pg_cjk_parser.control /root/parser/
COPY Makefile /root/parser/
COPY pg_cjk_parser--0.0.1.sql /root/parser/
COPY zht2zhs.h /root/parser/
RUN make clean && make install

FROM postgres:$POSTGRES_VERSION
ARG POSTGRES_VERSION=16
COPY --from=build /root/parser/pg_cjk_parser.bc /usr/lib/postgresql/$POSTGRES_VERSION/lib/bitcode
COPY --from=build /root/parser/pg_cjk_parser.so /usr/lib/postgresql/$POSTGRES_VERSION/lib
COPY --from=build /root/parser/pg_cjk_parser--0.0.1.sql /usr/share/postgresql/$POSTGRES_VERSION/extension
COPY --from=build /root/parser/pg_cjk_parser.control /usr/share/postgresql/$POSTGRES_VERSION/extension
20 changes: 20 additions & 0 deletions Dockerfile_alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

ARG POSTGRES_VERSION=16
FROM postgres:$POSTGRES_VERSION-alpine3.18 as build
ARG POSTGRES_VERSION=16
RUN apk update && apk add musl-dev icu-dev llvm15-dev clang15 make

RUN mkdir -p /root/parser
WORKDIR /root/parser
COPY pg_cjk_parser.c /root/parser/
COPY pg_cjk_parser.control /root/parser/
COPY Makefile /root/parser/
COPY pg_cjk_parser--0.0.1.sql /root/parser/
COPY zht2zhs.h /root/parser/
RUN make clean && make install

FROM postgres:$POSTGRES_VERSION-alpine3.18
COPY --from=build /root/parser/pg_cjk_parser.bc /usr/local/lib/postgresql/bitcode
COPY --from=build /root/parser/pg_cjk_parser.so /usr/local/lib/postgresql
COPY --from=build /root/parser/pg_cjk_parser--0.0.1.sql /usr/local/share/postgresql/extension
COPY --from=build /root/parser/pg_cjk_parser.control /usr/local/share/postgresql/extension
18 changes: 18 additions & 0 deletions Dockerfile_pg11
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

FROM postgres:11-alpine3.13 as build
RUN apk update && apk add build-base postgresql postgresql-dev gcc make clang

RUN mkdir -p /root/parser
WORKDIR /root/parser
COPY pg_cjk_parser.c /root/parser/
COPY pg_cjk_parser.control /root/parser/
COPY Makefile /root/parser/
COPY pg_cjk_parser--0.0.1.sql /root/parser/
COPY zht2zhs.h /root/parser/
RUN make clean && make install

FROM postgres:11-alpine3.13
COPY --from=build /root/parser/pg_cjk_parser.bc /usr/local/lib/postgresql/bitcode
COPY --from=build /root/parser/pg_cjk_parser.so /usr/local/lib/postgresql
COPY --from=build /root/parser/pg_cjk_parser--0.0.1.sql /usr/local/share/postgresql/extension
COPY --from=build /root/parser/pg_cjk_parser.control /usr/local/share/postgresql/extension
14 changes: 0 additions & 14 deletions Dockerfile_pg12

This file was deleted.

14 changes: 0 additions & 14 deletions Dockerfile_pg16

This file was deleted.

20 changes: 15 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ You can build pg_cjk_parser in a docker container.

To build this extension for PostgreSQL 12
```bash
docker build -t postgres:12-dev . -f Dockerfile_pg12
docker build -t postgres:12-dev . --build-arg POSTGRES_VERSION=12 -f Dockerfile_alpine
```

To build this extension for PostgreSQL 16
```bash
docker build -t postgres:12-dev . -f Dockerfile_pg16
docker build -t postgres:16-dev . --build-arg POSTGRES_VERSION=16 -f Dockerfile_alpine
```

To build this extension for PostgreSQL 11
```bash
docker build -t postgres:11-dev . -f Dockerfile_pg11
```

4. Run the following command
Expand Down Expand Up @@ -145,12 +150,17 @@ Now you can execute the sql demonstrated in the introduction section to see the
There is a Dockerfile in this repository which helps you build a docker image based on postgres:12.

```bash
docker build -t postgres:12-dev . -f Dockerfile_pg12
docker build -t postgres:12-dev . --build-arg POSTGRES_VERSION=12 -f Dockerfile_alpine
```

or you can build a docker image based on postgres:16.
```bash
docker build -t postgres:16-dev . --build-arg POSTGRES_VERSION=16 -f Dockerfile_alpine
```

There is also a Dockerfile in this repository which helps you build a docker image based on postgres:16.
or you can build a docker image based on postgres:11.
```bash
docker build -t postgres:16-dev . -f Dockerfile_pg16
docker build -t postgres:11-dev . -f Dockerfile_pg11
```

If you use this image to start an instance of postgres:12, all you need to do is to create the extension, search parser and configuration in pgAdmin.
Expand Down
4 changes: 4 additions & 0 deletions pg_cjk_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,10 @@ typedef struct
#define TS_NO false
#endif

#if PG_VERSION_NUM < 120000
#define pg_strtoint32(val) pg_atoi(val, sizeof(int32), 0)
#endif

/*
* TS_execute callback for matching a tsquery operand to headline words
*
Expand Down
78 changes: 78 additions & 0 deletions postgres-11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

docker run --name postgres11 -e POSTGRES_PASSWORD=password -d postgres:11-dev
sleep 5
OUTPUT=$(docker exec postgres11 psql -U postgres -c 'CREATE EXTENSION pg_cjk_parser;')
echo $OUTPUT
if [[ "$OUTPUT" != "CREATE EXTENSION" ]];
then
docker stop postgres11 && docker rm postgres11
exit 1
fi
docker exec postgres11 psql -U postgres -c "CREATE TEXT SEARCH PARSER public.pg_cjk_parser (START = prsd2_cjk_start, GETTOKEN = prsd2_cjk_nexttoken, END = prsd2_cjk_end, LEXTYPES = prsd2_cjk_lextype, HEADLINE = prsd2_cjk_headline); CREATE TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ( PARSER = pg_cjk_parser ); SET default_text_search_config = 'public.config_2_gram_cjk';"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR asciihword WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR cjk WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR email WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR asciiword WITH english_stem;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR entity WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR file WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR float WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR host WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR hword WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR hword_asciipart WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR hword_numpart WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR hword_part WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR int WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR numhword WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR numword WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR protocol WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR sfloat WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR tag WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR uint WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR url WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR url_path WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR version WITH simple;"

docker exec postgres11 psql -U postgres -c "ALTER TEXT SEARCH CONFIGURATION public.config_2_gram_cjk ADD MAPPING FOR word WITH simple;"

OUTPUT=$(docker exec postgres11 psql -U postgres -c "SET default_text_search_config = 'public.config_2_gram_cjk'; SELECT to_tsvector('Doraemnon Nobita「ドラえもん のび太の牧場物語」多拉A梦 野比大雄χΨψΩω'), to_tsquery('のび太'), to_tsquery('野比大雄');")
echo $OUTPUT
if [[ "$OUTPUT" != *"| 'のび' & 'び太' | '野比' & '比大' & '大雄'"* ]];
then
echo "Chinese/Japanese not splitted into 2-grams"
docker stop postgres11 && docker rm postgres11
exit 1
fi

OUTPUT=$(docker exec postgres11 psql -U postgres -c "SET default_text_search_config = 'public.config_2_gram_cjk'; SELECT to_tsvector('大韩民国개인정보의 수집 및 이용 목적(「개인정보 보호법」 제15조)'), to_tsquery('「大韩民国개인정보');")
echo $OUTPUT
if [[ "$OUTPUT" != *"'「' & '大韩' & '韩民' & '民国' & '国개' & '개인' & '인정' & '정보'"* ]];
then
echo "Korean not splitted into 2-grams"
docker stop postgres11 && docker rm postgres11
exit 1
fi

docker stop postgres11 && docker rm postgres11
Loading

0 comments on commit 2f1a721

Please sign in to comment.