# ===============================================
# Reconciliation Service Dockerfile
# 对账管理服务 Docker配置
# ===============================================

FROM eclipse-temurin:17-jdk-alpine AS builder

WORKDIR /app

# 复制Maven相关文件
COPY pom.xml .
COPY settings.xml ./

# 复制源码
COPY src ./src

# 设置Maven镜像加速
RUN sed -i 's|https://repo.maven.apache.org/maven2|https://maven.aliyun.com/repository/public|g' /usr/share/maven/conf/settings.xml || true

# 构建应用
RUN mvn clean package -DskipTests -q

# ===============================================
# 运行阶段
# ===============================================
FROM eclipse-temurin:17-jre-alpine

WORKDIR /app

# 添加非root用户
RUN addgroup -S erp && adduser -S erp -G erp

# 复制构建产物
COPY --from=builder /app/target/*.jar app.jar

# 复制SQL迁移脚本
COPY --from=builder /app/src/main/resources/db ./db

# 设置权限
RUN chown -R erp:erp /app

USER erp

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD wget -q --spider http://localhost:8018/actuator/health || exit 1

# JVM参数优化
ENV JAVA_OPTS="-Xms256m -Xmx512m -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

EXPOSE 8018

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
