说明
根据https://github.com/halo-dev/halo项目,自动化构建镜像。
Dockerfile
直接上Dockerfile,使用说明及注意事项在后面
FROM alpine:3.18.2 as builder
# 安装基础环境
RUN apk update && \
apk add openjdk17 nodejs npm make bash && \
npm config set registry https://registry.npmmirror.com && \
npm -g i pnpm
ADD ./halo /halo
WORKDIR /halo
# RUN version=$(grep -oP 'version=\K[^ ]+' gradle.properties | cut -d'=' -f2)
RUN sed -i 's/services.gradle.org\/distributions/mirrors.cloud.tencent.com\/gradle/g' gradle/wrapper/gradle-wrapper.properties
# 由于插件需要从github下载,正常情况会下载失败
# 所以把需要提前安装的插件放到halo/application/src/main/resources/presets/plugins目录下(需要手动创建)
# 如果不想安装任何插件,只需创建plugin目录就好
# 如果可以正常访问github,不要创建plugins目录,会自动下载默认插件
# mkdir -p halo/application/src/main/resources/presets/plugins
RUN if ls ./application/src/main/resources/presets/plugins > /dev/null 2>&1; \
then \
make -C console build && \
./gradlew clean build -x check; \
else \
make -C console build && \
./gradlew downloadPluginPresets && \
./gradlew clean build -x check; \
fi
RUN cd application/build/libs && \
java -Djarmode=layertools -jar application-*.jar extract
################################
FROM eclipse-temurin:17-jre
WORKDIR /application
COPY --from=builder /halo/application/build/libs/dependencies/ ./
COPY --from=builder /halo/application/build/libs/spring-boot-loader/ ./
COPY --from=builder /halo/application/build/libs/snapshot-dependencies/ ./
COPY --from=builder /halo/application/build/libs/application/ ./
ENV JVM_OPTS="-Xmx256m -Xms256m" \
HALO_WORK_DIR="/root/.halo2" \
SPRING_CONFIG_LOCATION="optional:classpath:/;optional:file:/root/.halo2/" \
TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo $TZ > /etc/timezone
ENTRYPOINT ["sh", "-c", "java ${JVM_OPTS} org.springframework.boot.loader.JarLauncher ${0} ${@}"]
使用说明
把Dockerfile文件和halo项目放到同级目录,根据自己的网络环境,决定是否创建plugins目录,
目录结构如下:
build-halo2
├── Dockerfile
└── halo
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── ..........
├── platform
└── settings.gradle
10 directories, 12 files
# 构建镜像
[root@jorelSpace build-halo2]# docker build -t halo:2.10.2 .
注意事项
由于插件需要从
github
下载,如果连接github
超时,只需要在halo项目的application/src/main/resources
目录中创建presets/plugins
目录,即可跳过联网下载插件
如果要预置插件,把下载好的插件放入到创建的plugins
目录中即可。
创建目录命令如下:
[root@jorelSpace build-halo2]# mkdir -p halo/application/src/main/resources/presets/plugins