Files
diary-news/backend/Dockerfile.postgres

68 lines
2.4 KiB
Docker
Raw Normal View History

FROM postgres:16-bookworm
# zhparser 中文分词扩展 — 全源码编译
# Debian / PGDG 仓库里都没有 postgresql-16-zhparser / libscws-dev / scws 包,
# 必须从 GitHub 拉源码自己 make。
#
# 关键点:scws 仓库年代久远,自带的 autotools 脚本跟新 automake (>=1.16) 不兼容
# (Makefile.am:24: error: '#' comment at start of rule is unportable)
# 所以绕过 autotools,直接 gcc 编译 libscws/*.c 然后 ld 拼 .so
#
# 步骤:
# 1) 装编译工具链 + PG dev headers
# 2) 手编 libscws(绕过 autotools)+ 装到 /usr/local
# 3) 编译 zhparser(PGXS 扩展,直接 make)
#
# build 时间:~3-5 分钟
# 产物:
# /usr/local/lib/libscws.so*
# /usr/local/include/scws/*.h
# /usr/lib/postgresql/16/lib/zhparser.so
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
build-essential \
git \
ca-certificates \
# pg_config / pgxs(编译 PG 扩展用)
postgresql-server-dev-16; \
rm -rf /var/lib/apt/lists/*
# 2) 手编 libscws(绕过 autotools — Makefile.am 不兼容新 automake)
RUN set -eux; \
cd /tmp; \
git clone --depth 1 https://github.com/hightman/scws.git; \
cd scws/libscws; \
# scws 的 autotools 会生成 version.h,手编自己造
printf '#define SCWS_VERSION "1.2.3"\n#define SCWS_BUILT __DATE__ " " __TIME__\n' > version.h; \
# 编译所有 .c
gcc -c -O2 -fPIC -Wall \
charset.c crc32.c pool.c scws.c xdict.c darray.c rule.c lock.c xdb.c xtree.c; \
# 装头文件
mkdir -p /usr/local/include/scws; \
cp charset.h crc32.h pool.h scws.h xdict.h darray.h rule.h xdb.h xtree.h version.h /usr/local/include/scws/; \
# 拼 .so
gcc -shared -Wl,-soname,libscws.so.1 -o libscws.so.1.0.0 *.o -lc; \
cp libscws.so.1.0.0 /usr/local/lib/; \
ln -sf libscws.so.1.0.0 /usr/local/lib/libscws.so.1; \
ln -sf libscws.so.1.0.0 /usr/local/lib/libscws.so; \
ldconfig; \
cd /; \
rm -rf /tmp/scws
# 3) 编译 zhparser(PGXS — 直接 make)
# 注意:zhparser 内部用 #include <scws/scws.h>,需要 /usr/local/include 在 CPATH 上
ENV CPATH=/usr/local/include
RUN set -eux; \
cd /tmp; \
git clone --depth 1 https://github.com/amutu/zhparser.git; \
cd zhparser; \
make -j$(nproc); \
make install; \
cd /; \
rm -rf /tmp/zhparser
# 验证
RUN ls -la /usr/lib/postgresql/16/lib/zhparser.so /usr/local/lib/libscws.so* 2>&1 | head -5