java_demo/Dockerfile
2025-06-24 10:27:20 +08:00

39 lines
1.2 KiB
Docker
Raw Permalink 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.

# 方案3使用本地已有的镜像你的服务器上已有 amazoncorretto:17-alpine-jdk
# 直接使用现有镜像作为运行时镜像,简化构建过程
FROM amazoncorretto:17-alpine-jdk
# 安装curl用于健康检查
RUN apk add --no-cache curl
# 设置时区
ENV TZ=Asia/Shanghai
RUN apk add --no-cache tzdata && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 创建非root用户
RUN addgroup -g 1000 spring && adduser -u 1000 -G spring -s /bin/sh -D spring
# 创建应用目录
WORKDIR /app
# 直接复制已构建的jar文件在Jenkins中已经通过Maven构建完成
COPY target/*.jar app.jar
# 创建日志目录
RUN mkdir -p /app/logs && chown -R spring:spring /app
# 切换到非root用户
USER spring
# 暴露端口
EXPOSE 8080
# 健康检查 - 使用Spring Boot Actuator标准端点
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD curl -f http://localhost:8080/actuator/health || curl -f http://localhost:8080/api/health || exit 1
# JVM调优参数
ENV JAVA_OPTS="-server -Xms256m -Xmx512m -XX:+UseG1GC -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"
# 启动应用
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar"]