Skip to content

Commit 45cecf5

Browse files
committed
Merge branch '3.3.x'
Closes gh-42114
2 parents b05336d + 8d44fd5 commit 45cecf5

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/class-data-sharing.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ This will cause the buildpack to do a training run of the application, save the
1515

1616
The Paketo Buildpack for Spring Boot https://github.com/paketo-buildpacks/spring-boot?tab=readme-ov-file#configuration[documentation] has information on other configuration options that can be enabled with builder environment variables, like `CDS_TRAINING_JAVA_TOOL_OPTIONS` that allows to override the default `JAVA_TOOL_OPTIONS`, only for the CDS training run.
1717

18+
[[howto.class-data-sharing.dockerfiles]]
19+
== Packaging an Application Using CDS and Dockerfiles
20+
21+
If you don't want to use Cloud Native Buildpacks, it is also possible to use CDS with a `Dockerfile`.
22+
For more information about that, please see the xref:reference:packaging/container-images/dockerfiles.adoc#packaging.container-images.dockerfiles.cds[Dockerfiles reference documentation].
23+
1824
[[howto.class-data-sharing.training-run-configuration]]
1925
== Preventing Remote Services Interaction During the Training Run
2026

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[packaging.container-images.dockerfiles]]
22
= Dockerfiles
33

4-
While it is possible to convert a Spring Boot uber jar into a Docker image with just a few lines in the Dockerfile, using the xref:packaging/container-images/efficient-images.adoc#packaging.container-images.efficient-images.layering[layering feature] will result in an optimized image.
4+
While it is possible to convert a Spring Boot uber jar into a Docker image with just a few lines in the `Dockerfile`, using the xref:packaging/container-images/efficient-images.adoc#packaging.container-images.efficient-images.layering[layering feature] will result in an optimized image.
55
When you create a jar containing the layers index file, the `spring-boot-jarmode-tools` jar will be added as a dependency to your jar.
66
With this jar on the classpath, you can launch your application in a special mode which allows the bootstrap code to run something entirely different from your application, for example, something that extracts the layers.
77

@@ -28,32 +28,12 @@ Available commands:
2828
help Help about any command
2929
----
3030

31-
The `extract` command can be used to easily split the application into layers to be added to the Dockerfile.
32-
Here is an example of a Dockerfile using `jarmode`.
31+
The `extract` command can be used to easily split the application into layers to be added to the `Dockerfile`.
32+
Here is an example of a `Dockerfile` using `jarmode`.
3333

3434
[source,dockerfile]
3535
----
36-
# Perform the extraction in a separate builder container
37-
FROM bellsoft/liberica-openjre-debian:17-cds AS builder
38-
WORKDIR /builder
39-
# This points to the built jar file in the target folder
40-
# Adjust this to 'build/libs/*.jar' if you're using Gradle
41-
ARG JAR_FILE=target/*.jar
42-
# Copy the jar file to the working directory and rename it to application.jar
43-
COPY ${JAR_FILE} application.jar
44-
# Extract the jar file using an efficient layout
45-
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted
46-
47-
# This is the runtime container
48-
FROM bellsoft/liberica-openjre-debian:17-cds
49-
WORKDIR /application
50-
# Copy the extracted jar contents from the builder container into the working directory in the runtime container
51-
# Every copy step creates a new docker layer
52-
# This allows docker to only pull the changes it really needs
53-
COPY --from=builder /builder/extracted/dependencies/ ./
54-
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
55-
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
56-
COPY --from=builder /builder/extracted/application/ ./
36+
include::reference:partial$dockerfile[]
5737
# Start the application jar - this is not the uber jar used by the builder
5838
# This jar only contains application code and references to the extracted jar files
5939
# This layout is efficient to start up and CDS friendly
@@ -67,10 +47,31 @@ Assuming the above `Dockerfile` is in the current directory, your Docker image c
6747
$ docker build --build-arg JAR_FILE=path/to/myapp.jar .
6848
----
6949

70-
This is a multi-stage Dockerfile.
50+
This is a multi-stage `Dockerfile`.
7151
The builder stage extracts the directories that are needed later.
7252
Each of the `COPY` commands relates to the layers extracted by the jarmode.
7353

74-
Of course, a Dockerfile can be written without using the `jarmode`.
54+
Of course, a `Dockerfile` can be written without using the `jarmode`.
7555
You can use some combination of `unzip` and `mv` to move things to the right layer but `jarmode` simplifies that.
7656
Additionally, the layout created by the `jarmode` is CDS friendly out of the box.
57+
58+
59+
60+
[[packaging.container-images.dockerfiles.cds]]
61+
== CDS
62+
63+
If you want to additionally enable xref:reference:packaging/class-data-sharing.adoc[CDS], you can use this `Dockerfile`:
64+
[source,dockerfile]
65+
----
66+
include::reference:partial$dockerfile[]
67+
# Execute the CDS training run
68+
RUN java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar application.jar
69+
# Start the application jar with CDS enabled - this is not the uber jar used by the builder
70+
# This jar only contains application code and references to the extracted jar files
71+
# This layout is efficient to start up and CDS friendly
72+
ENTRYPOINT ["java", "-XX:SharedArchiveFile=application.jsa", "-jar", "application.jar"]
73+
----
74+
75+
This is mostly the same as the above `Dockerfile`.
76+
As the last steps, it creates the CDS archive by doing a training run and passes the CDS parameter to `java -jar`.
77+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Perform the extraction in a separate builder container
2+
FROM bellsoft/liberica-openjre-debian:17-cds AS builder
3+
WORKDIR /builder
4+
# This points to the built jar file in the target folder
5+
# Adjust this to 'build/libs/*.jar' if you're using Gradle
6+
ARG JAR_FILE=target/*.jar
7+
# Copy the jar file to the working directory and rename it to application.jar
8+
COPY ${JAR_FILE} application.jar
9+
# Extract the jar file using an efficient layout
10+
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted
11+
12+
# This is the runtime container
13+
FROM bellsoft/liberica-openjre-debian:17-cds
14+
WORKDIR /application
15+
# Copy the extracted jar contents from the builder container into the working directory in the runtime container
16+
# Every copy step creates a new docker layer
17+
# This allows docker to only pull the changes it really needs
18+
COPY --from=builder /builder/extracted/dependencies/ ./
19+
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
20+
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
21+
COPY --from=builder /builder/extracted/application/ ./

0 commit comments

Comments
 (0)