记录一下Apache Answer系统使用自定义UI的过程。

目前已经放弃使用外置的方案了,不知道为什么页面渲染出现的HTML显示为空白。

下面主要记录docker打包部署的实现,我使用的是自建git库+拉取+docker构建的方式。

1、拉取git,下面的git库路径改用自己的库路径。

# 首次
git clone https://github.com/apache/answer.git
cd answer

# 以后更新
git fetch --all
git reset --hard origin/main   # 确保是干净工作区(会丢本地未提交改动)

# 构建镜像(使用 commit 短 SHA 做 tag 方便回滚)
sha=$(git rev-parse --short HEAD)
docker build -t my-answer:$sha -t my-answer:latest .

# 启动(最简单方式)
docker run -d --name answer --restart=on-failure -p 9080:80 -v answer-data:/data my-answer:$sha

2、如果服务器没有安装nodejs的话会报错。

安装Node.js 20
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -    #添加NodeSource仓库
sudo yum install -y nodejs   #安装Node.js
node -v    # 验证安装 应该输出 v20.x.x 或更高
npm -v
安装pnpm 9
sudo npm install -g pnpm@9    # 使用npm安装
pnpm -v     # 验证pnpm安装  应该输出 9.x.x 或更高

3、一定要进到ui目录先打包生成前端

pnpm i
pnpm build

4、使用docker构建时出现下面的错误,所以我根据AI的建议变更了Dockerfile的内容。

88.41 .../core-js@3.39.0/node_modules/core-js postinstall: > https://opencollective.com/core-js 
88.41 .../core-js@3.39.0/node_modules/core-js postinstall: > https://patreon.com/zloirock 
88.41 .../core-js@3.39.0/node_modules/core-js postinstall: > https://boosty.to/zloirock 
88.41 .../core-js@3.39.0/node_modules/core-js postinstall: > bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz 
88.41 .../core-js@3.39.0/node_modules/core-js postinstall: I highly recommend reading this: https://github.com/zloirock/core-js/blob/master/docs/2023-02-14-so-whats-next.md 
88.41 .../node_modules/core-js-pure postinstall: Done
88.42 .../core-js@3.39.0/node_modules/core-js postinstall: Done
89.04 .../node_modules/@swc/core postinstall: Failed
94.49 Segmentation fault (core dumped)
94.50  ELIFECYCLE  Command failed with exit code 139.
94.51 build failed exit status 139
------
Dockerfile:41
--------------------
  39 |     
  40 |     RUN chmod 755 answer
  41 | >>> RUN ["/bin/bash","-c","script/build_plugin.sh"]
  42 |     RUN cp answer /usr/bin/answer
  43 |     
--------------------
ERROR: failed to solve: process "/bin/bash -c script/build_plugin.sh" did not complete successfully: exit code: 1
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

FROM golang:1.23-bullseye AS golang-builder
LABEL maintainer="linkinstar@apache.org"

ARG GOPROXY
# ENV GOPROXY ${GOPROXY:-direct}
# ENV GOPROXY=https://proxy.golang.com.cn,direct

ENV GOPATH /go
ENV GOROOT /usr/local/go
ENV PACKAGE github.com/apache/answer
ENV BUILD_DIR ${GOPATH}/src/${PACKAGE}
ENV ANSWER_MODULE ${BUILD_DIR}

ARG TAGS="sqlite sqlite_unlock_notify"
ENV TAGS "bindata timetzdata $TAGS"
ARG CGO_EXTRA_CFLAGS

COPY . ${BUILD_DIR}
WORKDIR ${BUILD_DIR}
RUN apt-get update && apt-get install -y build-essential git bash curl \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g pnpm@9.7.0 \
    && make clean build

RUN chmod 755 answer
RUN ["/bin/bash","-c","script/build_plugin.sh"]
RUN cp answer /usr/bin/answer

RUN mkdir -p /data/uploads && chmod 777 /data/uploads \
    && mkdir -p /data/i18n && cp -r i18n/*.yaml /data/i18n

FROM debian:bullseye-slim
LABEL maintainer="linkinstar@apache.org"

ARG TIMEZONE
ENV TIMEZONE=${TIMEZONE:-"Asia/Shanghai"}

RUN apt-get update \
    && apt-get install -y \
        bash \
        ca-certificates \
        curl \
        dumb-init \
        gettext \
        openssh-client \
        sqlite3 \
        gnupg \
        tzdata \
    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \
    && rm -rf /var/lib/apt/lists/*

COPY --from=golang-builder /usr/bin/answer /usr/bin/answer
COPY --from=golang-builder /data /data
COPY /script/entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh

VOLUME /data
EXPOSE 80
ENTRYPOINT ["/entrypoint.sh"]

5、构建

docker build -t my-answer:latest .

6、启动

docker run -d -p 9080:80 -v answer-data:/data --name answer my-answer:latest

搞这个在windows环境下不容易

:hand_with_index_finger_and_thumb_crossed:,不错,借鉴一下。

Ubuntu的安装Nodejs 20

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v