Files
server/Dockerfile.robust
2025-11-01 15:36:47 +08:00

90 lines
2.5 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ===== 构建阶段 =====
# 使用Maven镜像进行构建
FROM maven:3.8.4-openjdk-8-slim AS builder
# 设置工作目录
WORKDIR /build
# 复制settings.xml和pom.xml文件
COPY settings.xml /root/.m2/
COPY pom.xml .
# 设置Maven仓库权限
RUN mkdir -p /root/.m2/repository && \
chown -R root:root /root/.m2
# 下载依赖利用Docker缓存层和国内镜像
RUN mvn dependency:go-offline -B -s /root/.m2/settings.xml
# 复制源代码
COPY src ./src
# 构建应用(使用国内镜像加速)
RUN mvn clean package -DskipTests -B -s /root/.m2/settings.xml
# ===== 运行阶段 =====
# 使用OpenJDK 8作为基础镜像
FROM openjdk:8-jre-alpine AS runtime
# 设置维护者信息
LABEL maintainer="digital-archive-team"
# 配置多个Alpine镜像源以提高下载成功率
RUN echo "https://mirrors.aliyun.com/alpine/v3.18/main/" > /etc/apk/repositories && \
echo "https://mirrors.aliyun.com/alpine/v3.18/community/" >> /etc/apk/repositories && \
echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.18/main/" >> /etc/apk/repositories && \
echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.18/community/" >> /etc/apk/repositories
# 更新包索引并安装必要的系统依赖和Tesseract OCR
RUN apk update && \
apk add --no-cache \
tesseract \
tesseract-data-chi_sim \
tesseract-data-chi_tra \
tesseract-data-eng \
tesseract-ocr \
tini \
curl \
bash \
&& rm -rf /var/cache/apk/*
# 设置环境变量
ENV JAVA_OPTS="-Xmx2g -Xms1g -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
ENV SPRING_PROFILES_ACTIVE=prod
# 创建应用用户和目录
RUN addgroup -g 1001 app \
&& adduser -D -s /bin/sh -u 1001 -G app app \
&& mkdir -p /app/data/upload \
&& mkdir -p /app/data/temp \
&& mkdir -p /app/data/unzip \
&& mkdir -p /app/data/images \
&& mkdir -p /app/data/reports \
&& mkdir -p /app/data/elasticsearch \
&& mkdir -p /app/logs \
&& chown -R app:app /app
# 设置工作目录
WORKDIR /app
# 从构建阶段复制jar文件
COPY --from=builder /build/target/point-strategy-*.jar app.jar
# 复制配置文件(如果需要覆盖默认配置)
# COPY application-prod.yml application-prod.yml
# 切换到非root用户
USER app
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:9081/point-strategy/actuator/health || exit 1
# 暴露端口
EXPOSE 9081
# 使用tini作为init进程
ENTRYPOINT ["/sbin/tini", "--"]
# 启动应用
CMD ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]