|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# Stage 1: Builder |
| 3 | +# This stage builds the application and its dependencies. |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +FROM maven:3.9-eclipse-temurin-21-alpine AS builder |
| 6 | + |
| 7 | +WORKDIR /app |
| 8 | + |
| 9 | +# Copy pom.xml and download dependencies. This will be cached until pom.xml |
| 10 | +# changes, speeding up the build process. |
| 11 | +COPY pom.xml . |
| 12 | +RUN mvn dependency:go-offline -B |
| 13 | + |
| 14 | +# Copy source code and build the application, skipping tests for faster builds. |
| 15 | +COPY src ./src |
| 16 | +RUN mvn clean package -DskipTests |
| 17 | + |
| 18 | +# ------------------------------------------------------------------------------ |
| 19 | +# Stage 2: Runtime |
| 20 | +# This stage creates the final, minimal image to run the application. |
| 21 | +# ------------------------------------------------------------------------------ |
| 22 | +FROM eclipse-temurin:21-jdk-alpine AS runtime |
| 23 | + |
| 24 | +WORKDIR /app |
| 25 | + |
| 26 | +# Install curl for health check |
| 27 | +RUN apk add --no-cache curl |
| 28 | + |
| 29 | +# Metadata labels for the image. These are useful for registries and inspection. |
| 30 | +LABEL org.opencontainers.image.title="🧪 RESTful Web Service with Spring Boot" |
| 31 | +LABEL org.opencontainers.image.description="Proof of Concept for a RESTful Web Service made with JDK 21 (LTS) and Spring Boot 3" |
| 32 | +LABEL org.opencontainers.image.licenses="MIT" |
| 33 | +LABEL org.opencontainers.image.source="https://github.com/nanotaboada/java.samples.spring.boot" |
| 34 | + |
| 35 | +# https://rules.sonarsource.com/docker/RSPEC-6504/ |
| 36 | + |
| 37 | +# Copy application JAR file from the builder stage |
| 38 | +COPY --from=builder /app/target/*.jar ./app.jar |
| 39 | + |
| 40 | +# Copy metadata docs for container registries (e.g.: GitHub Container Registry) |
| 41 | +COPY --chmod=444 README.md ./ |
| 42 | +COPY --chmod=555 assets/ ./assets/ |
| 43 | + |
| 44 | +# Copy entrypoint and healthcheck scripts |
| 45 | +COPY --chmod=555 scripts/entrypoint.sh ./entrypoint.sh |
| 46 | +COPY --chmod=555 scripts/healthcheck.sh ./healthcheck.sh |
| 47 | + |
| 48 | +# Add system user |
| 49 | +RUN addgroup -S spring && \ |
| 50 | + adduser -S -G spring spring |
| 51 | + |
| 52 | +USER spring |
| 53 | + |
| 54 | +EXPOSE 9000 |
| 55 | +EXPOSE 9001 |
| 56 | + |
| 57 | +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ |
| 58 | + CMD ["./healthcheck.sh"] |
| 59 | + |
| 60 | +ENTRYPOINT ["./entrypoint.sh"] |
| 61 | +CMD ["java", "-jar", "./app.jar"] |
0 commit comments