Video in progress

How would you write the simplest Dockerfile to run a Spring Boot application in Docker?

Copy the finished fat jar onto a JRE image and start it with java -jar. The official eclipse-temurin JRE image is a good base.

📖 Answer

First you build the jar:

./mvnw clean package -DskipTests

Then the Dockerfile:

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

Run it:

docker build -t shop-api:0.1 .
docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE=prod shop-api:0.1

This works, but it is still raw for prod. In practice you add the following next: a multi-stage build (so the jar is built inside Docker too), a non-root user, memory settings (-XX:MaxRAMPercentage), and a tag with an exact version. For a junior, it is enough to understand this simple version and know what is missing.