java_demo/Dockerfile
wangtianqi 98b08af4a3 fix: Remove network.host and simplify Buildx configuration
Issues fixed:
-  Remove --network=host (not allowed by daemon config)
-  Remove --mount=type=cache from Dockerfile (compatibility)
-  Simplify builder management and error handling
-  Keep caches on build failure for faster retries
-  Use standard Docker layer caching instead of BuildKit mount

This should resolve the 'granting entitlement network.host is not allowed' error.
2025-06-24 09:08:13 +08:00

76 lines
2.3 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.

# 使用多阶段构建优化镜像大小
FROM amazoncorretto:17-alpine-jdk as builder
# 设置工作目录
WORKDIR /app
# 设置Maven镜像源提高下载速度
RUN mkdir -p /root/.m2 && \
echo '<?xml version="1.0" encoding="UTF-8"?>' > /root/.m2/settings.xml && \
echo '<settings>' >> /root/.m2/settings.xml && \
echo ' <mirrors>' >> /root/.m2/settings.xml && \
echo ' <mirror>' >> /root/.m2/settings.xml && \
echo ' <id>aliyun</id>' >> /root/.m2/settings.xml && \
echo ' <name>Aliyun Central</name>' >> /root/.m2/settings.xml && \
echo ' <url>https://maven.aliyun.com/repository/central</url>' >> /root/.m2/settings.xml && \
echo ' <mirrorOf>central</mirrorOf>' >> /root/.m2/settings.xml && \
echo ' </mirror>' >> /root/.m2/settings.xml && \
echo ' </mirrors>' >> /root/.m2/settings.xml && \
echo '</settings>'
# 复制Maven配置文件和脚本利用Docker缓存
COPY pom.xml .
COPY .mvn .mvn
COPY mvnw .
# 给mvnw脚本添加执行权限
RUN chmod +x ./mvnw
# 首先只下载依赖利用Docker层缓存不使用mount缓存
RUN ./mvnw dependency:resolve -B -q
# 复制源代码
COPY src ./src
# 构建应用(简化构建过程)
RUN ./mvnw clean package -DskipTests -B -q
# 运行时镜像 - 使用更小的Alpine镜像
FROM amazoncorretto:17-alpine
# 安装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文件
COPY --from=builder /app/target/*.jar app.jar
# 创建日志目录
RUN mkdir -p /app/logs && chown -R spring:spring /app
# 切换到非root用户
USER spring
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD 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"]