Skip to content

Instantly share code, notes, and snippets.

@making
Last active December 1, 2023 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save making/35cfa52862e93793bad2b37b7c0e5135 to your computer and use it in GitHub Desktop.
Save making/35cfa52862e93793bad2b37b7c0e5135 to your computer and use it in GitHub Desktop.
Dockerfile for Spring Boot + CRaC
FROM azul/zulu-openjdk:21-jdk-crac-latest as builder
WORKDIR application
ADD ./.mvn .mvn/
ADD ./mvnw mvnw
ADD ./pom.xml pom.xml
ADD ./src src/
ADD ./.git .git/
RUN ./mvnw -V clean package -DskipTests --no-transfer-progress && \
cp target/*.jar application.jar && \
java -Djarmode=layertools -jar application.jar extract
FROM azul/zulu-openjdk:21-jdk-crac-latest
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
COPY entrypoint.sh ./
ENTRYPOINT ["bash", "/application/entrypoint.sh"]
#!/bin/bash
if [ "$CLEAN_CHECKPOINT" = "true" ] && [ -n "$CHECKPOINT_RESTORE_FILES_DIR" ] && [ -d "$CHECKPOINT_RESTORE_FILES_DIR" ]; then
rm -f $CHECKPOINT_RESTORE_FILES_DIR/*
fi
mkdir -p $CHECKPOINT_RESTORE_FILES_DIR
export JAVA_TOOL_OPTIONS="${JAVA_TOOL_OPTIONS} -XX:+ExitOnOutOfMemoryError"
if [ -z "$(ls $CHECKPOINT_RESTORE_FILES_DIR/core-*.img 2>/dev/null)" ]; then
echo "Save checkpoint to $CHECKPOINT_RESTORE_FILES_DIR" 1>&2
java -XX:CRaCCheckpointTo=$CHECKPOINT_RESTORE_FILES_DIR org.springframework.boot.loader.launch.JarLauncher &
sleep ${SLEEP_BEFORE_CHECKPOINT:-10}
jcmd org.springframework.boot.loader.launch.JarLauncher JDK.checkpoint
sleep ${SLEEP_AFTER_CHECKPOINT:-3}
else
echo "Restore checkpoint from $CHECKPOINT_RESTORE_FILES_DIR" 1>&2
fi
(echo 128 > /proc/sys/kernel/ns_last_pid) 2>/dev/null || while [ $(cat /proc/sys/kernel/ns_last_pid) -lt 128 ]; do :; done
java -XX:CRaCRestoreFrom=$CHECKPOINT_RESTORE_FILES_DIR &
JAVA_PID=$!
stop_java_app() {
kill -SIGTERM $JAVA_PID
}
trap stop_java_app SIGINT
wait $JAVA_PID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment