首页 > 解决方案 > 如何在多阶段 Docker 容器中构建 Postgres jsonlog

问题描述

我有一个多级 Docker 容器,我在其中构建 jsonlog。

# Build stage
FROM postgres:11.5 AS build-env
ADD . /jsonlog
RUN apt-get update && apt-get install -y build-essential libkrb5-dev libssl-dev libpq-dev postgresql-server-dev-all
RUN cd /jsonlog && make install
# Used find to see where the output files are being installed
# RUN find / -name "*json*"

# Final stage
FROM postgres:11.5-alpine
COPY --from=build-env /usr/lib/postgresql/11/lib/bitcode/jsonlog/jsonlog.bc /usr/lib/postgresql/11/lib/bitcode/jsonlog/jsonlog.bc
COPY --from=build-env /usr/lib/postgresql/11/lib/bitcode/jsonlog.index.bc /usr/lib/postgresql/11/lib/bitcode/jsonlog.index.bc
COPY --from=build-env /usr/lib/postgresql/11/lib/jsonlog.so /usr/lib/postgresql/11/lib/jsonlog.so

docker build好像还行 但是,当我执行 a 时docker run,会出现此错误。

LOG:  invalid value for parameter "log_destination": "jsonlog"
DETAIL:  Unrecognized key word: "jsonlog".
FATAL:  configuration file "/etc/postgresql/postgresql.conf" contains errors

jsonlog 代码来自我克隆的这个 repo, https://github.com/michaelpq/pg_plugins/tree/master/jsonlog 。我只将jsonlog目录安装到构建容器中。

postgresql.conf是香草,除了这些线。

log_destination = 'jsonlog'
logging_collector = on
shared_preload_libraries = 'jsonlog'

做多阶段容器的原因是为了摆脱构建依赖并拥有更小的容器。我想运行postgres:11.5-alpine,但postgres:11.5图像也出现错误。

更新

我试着摆脱多阶段,只把所有东西都放在这样的 1 个容器中。

FROM postgres:11.5
ADD . /jsonlog
RUN apt-get update && apt-get install -y build-essential libkrb5-dev libssl-dev libpq-dev postgresql-server-dev-all
RUN cd /jsonlog && make install

但这会导致确切的Unrecognized key word: "jsonlog"错误。

此外,我还尝试在 Postgres 9.5 容器中构建,并且发生了同样的事情。

标签: postgresqldocker

解决方案


据我了解,您只需将 log_destination 保留为“stderr”,并且 jsonlog 插件会覆盖 PG 中的 log_hook,并且神奇地,日志开始以 JSON 格式输出。

因此,AFAIK 唯一需要的改变是:

shared_preload_libraries = 'jsonlog'


推荐阅读