r/SpringBoot • • Jun 16 '24

Spring Into Docker: Containerizing Your Application Effectively

Learn how to effectively containerize a Spring Boot application using Docker and Docker Compose for streamlined deployment and scalability

spring #springboot #docker  #dockercompose

https://techsphere.dev/spring-boot-docker

23 Upvotes

5 comments sorted by

View all comments

4

u/swissbuechi Jun 16 '24 edited Jun 16 '24

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" ]

3

u/brainiac_nerd Jun 17 '24

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

I will update the article. Thanks.

1

u/Nice_Score_7552 Jun 17 '24

Thanks for your effort

2

u/Academic_Speed4839 Jun 16 '24

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

4

u/swissbuechi Jun 16 '24

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