|
1 | 1 | # Stage 1: Build the jar |
2 | | -FROM gradle:8.12-jdk21 AS build |
3 | | -# Copy source code into the container and set the ownership to 'gradle' user |
| 2 | +FROM gradle:8.14.3-jdk21-jammy AS build |
| 3 | + |
| 4 | +# Update system packages |
| 5 | +RUN apt-get update && \ |
| 6 | + apt-get upgrade -y && \ |
| 7 | + apt-get clean && \ |
| 8 | + rm -rf /var/lib/apt/lists/* |
| 9 | + |
| 10 | +# Copy source code and build |
4 | 11 | COPY --chown=gradle:gradle . /home/gradle/src |
5 | 12 | WORKDIR /home/gradle/src |
6 | 13 | RUN gradle build -x test --no-daemon |
7 | 14 |
|
8 | 15 | # Stage 2: Production image |
9 | | -FROM openjdk:21-slim AS production |
| 16 | +FROM openjdk:21-slim-bookworm AS production |
10 | 17 | EXPOSE 8080 |
11 | 18 |
|
12 | | -# Create a non-root user and group (using 'appuser' as an example) |
13 | | -RUN groupadd -r appgroup && useradd -r -g appgroup -m appuser |
| 19 | +# Update system packages and install fixed versions |
| 20 | +RUN apt-get update && \ |
| 21 | + apt-get upgrade -y && \ |
| 22 | + apt-get install -y --no-install-recommends \ |
| 23 | + libc6 \ |
| 24 | + util-linux \ |
| 25 | + && apt-get clean && \ |
| 26 | + rm -rf /var/lib/apt/lists/* |
| 27 | + |
| 28 | +# Create non-root user with fixed UID/GID |
| 29 | +RUN groupadd -r appgroup -g 10001 && \ |
| 30 | + useradd -r -g appgroup -u 10001 appuser && \ |
| 31 | + mkdir /app && \ |
| 32 | + chown 10001:10001 /app |
| 33 | + |
| 34 | +# Copy jar with specific name |
| 35 | +COPY --from=build --chown=10001:10001 /home/gradle/src/build/libs/*.jar /app/companieshouse.jar |
14 | 36 |
|
15 | | -# Create the /app directory and set permissions |
16 | | -RUN mkdir /app && chown appuser:appgroup /app |
| 37 | +WORKDIR /app |
| 38 | +USER 10001 |
17 | 39 |
|
18 | | -# Copy the jar file from the build stage into the production image |
19 | | -COPY --from=build /home/gradle/src/build/libs/*.jar /app/companieshouse-*.jar |
| 40 | +# Security-focused Java options |
| 41 | +ENV JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom \ |
| 42 | + -Djava.awt.headless=true \ |
| 43 | + -Dfile.encoding=UTF-8 \ |
| 44 | + -XX:+ExitOnOutOfMemoryError \ |
| 45 | + -XX:+UseContainerSupport \ |
| 46 | + -XX:MaxRAMPercentage=75.0 \ |
| 47 | + -Dspring.profiles.active=production \ |
| 48 | + -Dserver.tomcat.accesslog.enabled=true" |
20 | 49 |
|
21 | | -# Change to non-root user |
22 | | -USER appuser |
| 50 | +# Add healthcheck |
| 51 | +HEALTHCHECK --interval=30s --timeout=3s \ |
| 52 | + CMD curl -f http://localhost:8080/companieshouse/actuator/health || exit 1 |
23 | 53 |
|
24 | | -# Set the entrypoint to run the Java application |
25 | | -ENTRYPOINT ["java", "-jar", "/app/companieshouse-*.jar"] |
| 54 | +# Use specific jar name in entrypoint |
| 55 | +ENTRYPOINT ["java", "-jar", "/app/companieshouse.jar"] |
0 commit comments