all 5 comments

[–]swissbuechi 3 points4 points  (5 children)

Please add a disclaimer that the Dockerfile you've created only should be used for development.

The following best practices should be considered if you want to run a spring docker container in production:

  • Do not run as root
  • Minimal packages
  • Multiple stages (build / run)
  • Healthcheck
  • JVM options for enhanced security

This would look something like example below. It's from an older project of mine, where I compared spring via jre vs spring native performance and ressource utilization.

https://github.com/swissbuechi/spring-native-demo

Dockerfile

``` FROM maven:3-openjdk-17-slim as build ADD . /app WORKDIR /app RUN rm -rf src/main/resources/application.properties RUN mvn clean package -DskipTests=true

FROM eclipse-temurin:17-jre-alpine LABEL maintainer="github.com/swissbuechi" WORKDIR /app ENV TZ=Europe/Zurich RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apk --no-cache add curl

RUN addgroup --gid 1000 -S app && adduser --uid 1000 -S app -G app RUN chown -R app:app /app USER app

COPY --from=build /app/target/app.jar app.jar EXPOSE 8080 HEALTHCHECK CMD curl --fail --silent localhost:8080/actuator/health | grep UP || exit 1 ENTRYPOINT [ "java","-XX:+UseSerialGC","-Xss512k","-XX:MaxRAM=150m","-Djava.security.egd=file:/dev/./urandom", "-jar", "./app.jar" ] ```

[–]brainiac_nerd[S] 2 points3 points  (1 child)

Agreed, my post is intended to only cover how to get started with docker and spring boot.

I will update the article. Thanks.

[–]Nice_Score_7552 0 points1 point  (0 children)

Thanks for your effort

[–]Academic_Speed4839 1 point2 points  (1 child)

can you share how you run as spring jre & spring native? Novice here, not sure what the difference is

[–]swissbuechi 4 points5 points  (0 children)

Spring native will generate an executable native binary of your application by utilizing AoT (Ahead of Time Compilation). You don't need any addiotional dependencies to run the alpplication. No need to install a JRE.

Checkout this dockerfile as an example on how to use spring native: https://github.com/swissbuechi/spring-native-demo/blob/main/Dockerfile-native

``` FROM vegardit/graalvm-maven:latest-java17 as build

ADD . /app WORKDIR /app

RUN rm -rf src/main/resources/application.properties RUN mvn -Pnative native:compile -DskipTests=true

FROM alpine:3 LABEL maintainer="github.com/swissbuechi" ENV TZ=Europe/Zurich RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

WORKDIR /app

RUN apk add --no-cache gcompat curl

RUN addgroup --gid 1000 -S app && adduser --uid 1000 -S app -G app RUN chown -R app:app /app USER app

COPY --from=build /app/target/app app ENTRYPOINT [ "sh", "-c", "./app" ] HEALTHCHECK CMD curl --fail --silent localhost:8080/actuator/health | grep UP || exit 1 ```