diff --git a/Dockerfile.checkpoint b/Dockerfile.checkpoint new file mode 100644 index 0000000..fa34abd --- /dev/null +++ b/Dockerfile.checkpoint @@ -0,0 +1,23 @@ +ARG JDK_NAME=zulu22.30.13-ca-crac-jdk22.0.1-linux_x64 + +FROM ubuntu:24.04 as builder +ARG JDK_NAME + +RUN apt-get update && apt-get install -y wget +RUN wget -O crac-jdk.tar.gz https://cdn.azul.com/zulu/bin/$JDK_NAME.tar.gz +RUN tar zxf ./crac-jdk.tar.gz -C /usr/share + +# End of builder + +FROM ubuntu:24.04 +ARG JDK_NAME +ARG FAT_JAR= + +ENV JDK_NAME=$JDK_NAME +ENV JAVA_HOME=/usr/share/$JDK_NAME + +COPY --from=builder /usr/share/${JDK_NAME} /usr/share/${JDK_NAME} +RUN ln -s $JAVA_HOME/bin/java /bin/ && ln -s $JAVA_HOME/bin/jcmd /bin/ +ADD target/example-spring-boot*.jar /example-spring-boot.jar +ENTRYPOINT [ "java", "-XX:CPUFeatures=generic", "-XX:CRaCCheckpointTo=/cr", "-jar", "/example-spring-boot.jar" ] + diff --git a/Dockerfile.restore b/Dockerfile.restore new file mode 100644 index 0000000..b7ec797 --- /dev/null +++ b/Dockerfile.restore @@ -0,0 +1,5 @@ +FROM example-spring-boot-checkpoint +ADD target/cr /cr +ENTRYPOINT [ "java", "-XX:CRaCRestoreFrom=/cr" ] + + diff --git a/README.md b/README.md index 1dbf7b9..e2ff0e4 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,38 @@ jcmd target/example-spring-boot-0.0.1-SNAPSHOT.jar JDK.checkpoint ``` $JAVA_HOME/bin/java -XX:CRaCRestoreFrom=cr ``` + +## Preparing a container image + +1. After building the project locally create an image to be checkpointed. +``` +docker build -f Dockerfile.checkpoint -t example-spring-boot-checkpoint . +``` + +2. Start a (detached) container that will be checkpointed. Note that we're mounting `target/cr` into the container. +``` +docker run -d --rm -v $(pwd)/target/cr:/cr --cap-add=CHECKPOINT_RESTORE --cap-add=SYS_PTRACE -p 8080:8080 --name example-spring-boot-checkpoint example-spring-boot-checkpoint +``` + +3. Validate that the container is up and running (here you could also perform any warm-up) +``` +curl localhost:8080 +Greetings from Spring Boot! +``` + +4. Checkpoint the running container +``` +docker exec -it example-spring-boot-checkpoint jcmd example-spring-boot JDK.checkpoint +``` + +5. Build another container image by adding the data from `target/cr` on top of the previous image and adjusting entrypoint: +``` +docker build -f Dockerfile.restore -t example-spring-boot . +``` + +6. (Optional) Start the application in the restored container and validate that it works +``` +docker run -it --rm -p 8080:8080 example-spring-boot +# In another terminal +curl localhost:8080 +```