Video in progress

Why does an application started with this Dockerfile not shut down cleanly on SIGTERM?

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/app.jar app.jar
ENTRYPOINT java -Xmx512m -jar app.jar

A shell-form ENTRYPOINT runs the application under /bin/sh -c: PID 1 is the shell, and Java is its child. The shell does not forward SIGTERM to Java, so the grace period runs out and the process is killed with SIGKILL.

📖 Answer

docker stop (or Kubernetes) sends SIGTERM to PID 1 in the container. In shell form, PID 1 is sh. It neither handles the signal itself nor passes it to its child. After 10 seconds (30 in Kubernetes) SIGKILL arrives, and Spring Boot's graceful shutdown never runs at all.

It is easy to check:

docker exec shop-api ps -ef
# PID 1: /bin/sh -c java -Xmx512m -jar app.jar   <- this is the problem

The fix is exec form:

ENTRYPOINT ["java", "-Xmx512m", "-jar", "app.jar"]

Now java itself is PID 1 and receives the signal directly.

If you still need a shell script (to assemble arguments from env vars, say), end the script with exec java ..., because exec replaces the shell with Java. This is also where the difference between CMD and ENTRYPOINT shows up: ENTRYPOINT is the main command, CMD holds its default arguments, and arguments given at the end of docker run replace CMD.