mkdir -pv ~/workspace/dockerfile/kiwix-serve
根据需求,下载指定版本的源码包即可。
示例:
cd ~/workspace/dockerfile/kiwix-serve
wget https://download.kiwix.org/release/kiwix-tools/kiwix-tools_linux-x86_64-3.7.0-2.tar.gz
tar zxvf kiwix-tools_linux-x86_64-3.7.0-2.tar.gz
cp -rpf ./kiwix-tools_linux-x86_64-3.7.0-2/kiwix-serve ./
rm -rf kiwix-tools_linux-x86_64-3.7.0-2.tar.gz kiwix-tools_linux-x86_64-3.7.0-2
vim ~/workspace/dockerfile/kiwix-serve/dockerfile
# 配置参数(指定基础镜像版本)
ARG BASE_TAG=15.5
# 基于 openSUSE Leap 构建
FROM opensuse/leap:$BASE_TAG
# 指定服务端口号
EXPOSE 10080
# 配置参数(用户组、用户、HOME目录、安装目录、安装版本等)
ARG GROUP_ID=1024 \
GROUP_NAME=kiwix \
USER_ID=1024 \
USER_NAME=kiwix \
USER_HOME=/home/kiwix \
USER_SHELL=/bin/bash
# 更新应用并安装基础应用、清除 zypper 缓存
RUN set -ex && \
zypper refresh && \
zypper update -y && \
zypper install -y curl && \
zypper clean
# 设置时区、新建用户组及用户、配置目录权限
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
groupadd $GROUP_NAME -g $GROUP_ID && \
useradd $USER_NAME -g $GROUP_ID -d $USER_HOME -s $USER_SHELL -u $USER_ID && \
mkdir -pv $USER_HOME && \
chown -R $GROUP_NAME.$USER_NAME $USER_HOME
# 配置环境变量
ARG KIWIX_SERVE_VERSION=3.7.0
ENV KIWIX_HOME=/opt/kiwix-serve/$KIWIX_SERVE_VERSION \
KIWIX_ZIM_FILE_DIR=/data
# 安装 Kiwix Server
COPY kiwix-serve $KIWIX_HOME/kiwix-serve
COPY entrypoint.sh $KIWIX_HOME/entrypoint.sh
RUN mkdir -pv $KIWIX_ZIM_FILE_DIR && \
chown -R $GROUP_NAME.$USER_NAME $KIWIX_HOME $KIWIX_ZIM_FILE_DIR && \
chmod -R 755 $KIWIX_HOME $KIWIX_ZIM_FILE_DIR
# 健康检查
HEALTHCHECK --interval=10s --timeout=3s CMD curl -f http://127.0.0.1:10080 || exit 1
# 设置工作目录
WORKDIR $KIWIX_HOME
# 设置启动用户
USER $GROUP_NAME:$USER_NAME
# 设置执行启动脚本
CMD ["/bin/bash","-c","./entrypoint.sh"]
其中
KIWIX_SERVE_VERSION
的值为下载的 kiwix-serve 二进制包版本,此处示例使用3.7.0
entrypoint.sh
文件vim ~/workspace/dockerfile/kiwix-serve/entrypoint.sh
#!/bin/bash
echo '===================================================================================================='
echo `date +%Y-%m-%d` `date +%H:%M:%S`
echo '============================== 开始启动 Kiwix Serve...... =============================='
echo 'Kiwix Serve 工作目录:' $KIWIX_HOME
echo 'Kiwix Serve 监听端口:10080'
echo 'ZIM 文件目录:' $KIWIX_ZIM_FILE_DIR
echo '----------------------------------------------------------------------------------------------------'
$KIWIX_HOME/kiwix-serve -p 10080 $KIWIX_ZIM_FILE_DIR/*.zim
chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
cd ~/workspace/dockerfile/kiwix-serve
docker build --no-cache -t hub.hty1024.com/hty1024/kiwix-serve:3.7.0 .
其中
hub.hty1024.com/hty1024/kiwix-serve
为镜像名称,3.7.0
为镜像 Tag(建议与二进制包版本一致)
1)准备 zim
存档文件
以下
data
目录的相关操作为从官方下载zim
存档文件,此处示例使用wikipedia_zh_computer_maxi_2024-05.zim
,实际操作时可按需下载其他文件
mkdir -pv ./data
cd ./data
wget https://download.kiwix.org/zim/wikipedia/wikipedia_zh_computer_maxi_2024-05.zim
chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
cd chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
2) 使用 docker-compose 运行容器
此处使用 docker compose 运行镜像,因此需要安装 docker-compose
vim ~/workspace/dockerfile/kiwix-serve/compose.yaml
version: "3.9"
services:
kiwix-serve:
image: hty1024/kiwix-serve:3.7.0
container_name: kiwix-serve
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- ./data:/data
ports:
- "10080:10080"
networks:
- app_network
networks:
app_network:
external: true
name: app_network
docker-compose up -d
3)浏览器访问 10080
端口,测试是否正常即可
mkdir -pv ~/workspace/dockerfile/kiwix-serve
根据需求,下载指定版本的源码包即可。
示例:
cd ~/workspace/dockerfile/kiwix-serve
wget https://download.kiwix.org/release/kiwix-tools/kiwix-tools_linux-x86_64-3.7.0-2.tar.gz
tar zxvf kiwix-tools_linux-x86_64-3.7.0-2.tar.gz
cp -rpf ./kiwix-tools_linux-x86_64-3.7.0-2/kiwix-serve ./
rm -rf kiwix-tools_linux-x86_64-3.7.0-2.tar.gz kiwix-tools_linux-x86_64-3.7.0-2
vim ~/workspace/dockerfile/kiwix-serve/dockerfile
# 配置参数(指定基础镜像版本)
ARG BASE_TAG=3.19.1
# 基于 AlpineLinux 构建
FROM alpine:$BASE_TAG
# 指定端口号
EXPOSE 10080
# 配置参数(用户组、用户、HOME目录、安装目录、安装版本等)
ARG GROUP_ID=1024 \
GROUP_NAME=kiwix \
USER_ID=1024 \
USER_NAME=kiwix \
USER_HOME=/home/kiwix \
USER_SHELL=/bin/bash
# 更新应用并安装基础应用
RUN set -ex && \
apk add tzdata && \
apk add --no-cache bash bash-doc bash-completion curl
# 设置时区、新建用户组及用户、配置目录权限
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
addgroup $GROUP_NAME -g $GROUP_ID && \
adduser $USER_NAME -G $GROUP_NAME -h $USER_HOME -s $USER_SHELL -u $USER_ID -D && \
mkdir -pv $USER_HOME && \
chown -R $GROUP_NAME.$USER_NAME $USER_HOME
# 配置环境变量
ARG KIWIX_SERVE_VERSION=3.7.0
ENV KIWIX_HOME=/opt/kiwix-serve/$KIWIX_SERVE_VERSION \
KIWIX_ZIM_FILE_DIR=/data
# 安装 Kiwix Server
COPY kiwix-serve $KIWIX_HOME/kiwix-serve
COPY entrypoint.sh $KIWIX_HOME/entrypoint.sh
RUN mkdir -pv $KIWIX_ZIM_FILE_DIR && \
chown -R $GROUP_NAME.$USER_NAME $KIWIX_HOME $KIWIX_ZIM_FILE_DIR && \
chmod -R 755 $KIWIX_HOME $KIWIX_ZIM_FILE_DIR
# 健康检查
HEALTHCHECK --interval=10s --timeout=3s CMD curl -f http://127.0.0.1:10080 || exit 1
# 设置工作目录
WORKDIR $KIWIX_HOME
# 设置启动用户
USER $GROUP_NAME:$USER_NAME
# 设置执行启动脚本
CMD ["/bin/bash","-c","./entrypoint.sh"]
其中
KIWIX_SERVE_VERSION
的值为下载的 kiwix-serve 二进制包版本,此处示例使用3.7.0
entrypoint.sh
文件vim ~/workspace/dockerfile/kiwix-serve/entrypoint.sh
#!/bin/bash
echo '===================================================================================================='
echo `date +%Y-%m-%d` `date +%H:%M:%S`
echo '============================== 开始启动 Kiwix Serve...... =============================='
echo 'Kiwix Serve 工作目录:' $KIWIX_HOME
echo 'Kiwix Serve 监听端口:10080'
echo 'ZIM 文件目录:' $KIWIX_ZIM_FILE_DIR
echo '----------------------------------------------------------------------------------------------------'
$KIWIX_HOME/kiwix-serve -p 10080 $KIWIX_ZIM_FILE_DIR/*.zim
chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
cd ~/workspace/dockerfile/kiwix-serve
docker build --no-cache -t hub.hty1024.com/hty1024/kiwix-serve:3.7.0-alpine .
其中
hub.hty1024.com/hty1024/kiwix-serve
为镜像名称,3.7.0-alpine
为镜像 Tag(建议与二进制包版本一致),后缀alpine
表示此镜像基于AlpineLinux
构建
1)准备 zim
存档文件
以下
data
目录的相关操作为从官方下载zim
存档文件,此处示例使用wikipedia_zh_computer_maxi_2024-05.zim
,实际操作时可按需下载其他文件
mkdir -pv ./data
cd ./data
wget https://download.kiwix.org/zim/wikipedia/wikipedia_zh_computer_maxi_2024-05.zim
chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
cd chown -R 1024.1024 ~/workspace/dockerfile/kiwix-serve
2) 使用 docker-compose 运行容器
此处使用 docker compose 运行镜像,因此需要安装 docker-compose
vim ~/workspace/dockerfile/kiwix-serve/compose.yaml
version: "3.9"
services:
kiwix-serve:
image: hty1024/kiwix-serve:3.7.0-alpine
container_name: kiwix-serve
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- ./data:/data
ports:
- "10080:10080"
networks:
- app_network
networks:
app_network:
external: true
name: app_network
docker-compose up -d
3)浏览器访问 10080
端口,测试是否正常即可