Skip to content

Commit abd7df7

Browse files
authored
Merge pull request #1538 from oracle-devrel/native-build
native image added
2 parents 61cedc6 + 2404e26 commit abd7df7

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ June 13, 2024 is June 13, 1924. Here are some events that occurred on this date:
8989
5. The German aerospace company Zeppelin began constructing the Hindenburg, a large passenger airship.
9090
</pre>
9191

92+
# Native image using GraalVM
93+
94+
GraalVM compiles your Java functions ahead of time into standalone binaries that start instantly, provide peak performance with no warmup, and use fewer resources. The key GraalVM benefits are: Low Resource Usage: Java applications compiled ahead-of-time by GraalVM require less memory and CPU to run.
95+
96+
<p>
97+
98+
To do this a <a href="./files/Dockerfile.native">Docker multi-stage build</a> is used. E.g.
99+
100+
<pre>
101+
docker build -f Dockerfile.native -t fra.ocir.io/&lt;YOUR OCI TENANCY NAMESPACE&gt;/helloworldai-java:2 .
102+
</pre>
103+
104+
The GraalVM compilation stage requires quite a bit resources from your localhost so in case for example using Rancher desktop
105+
think of increasing the CPU and memory for it to make the build faster.
106+
107+
<p>
108+
109+
In the <a href="./files/Dockerfile.native">Dockerfile.native</a> two things are important: Including the <a href="./files/reflection.json">reflection.json</a> with the proper function class name and passing <code>"-Djava.library.path=/lib"</code> in the container CMD along with the <code>"com.example.HelloAIFunction::handleRequest"</code> function handler.
110+
111+
<p>
112+
113+
After the build push the container to OCIR repo:
114+
115+
<pre>
116+
docker push fra.ocir.io/&lt;YOUR OCI TENANCY NAMESPACE&gt;/helloworldai-java:2
117+
</pre>
118+
119+
Finally deploy the container to your OCI Function by replacing the container using the Cloud UI by editing the function and changing the container from <code>helloworldai-java:1</code> to <code>helloworldai-java:2</code>. Then test it.
120+
121+
92122
# Useful Links
93123

94124
- [OCI Functions](https://docs.oracle.com/en-us/iaas/Content/Functions/Concepts/functionsoverview.htm)
@@ -99,6 +129,8 @@ June 13, 2024 is June 13, 1924. Here are some events that occurred on this date:
99129
- The Fn project is an open-source container-native serverless platform that you can run anywhere -- any cloud or on-premise. It’s easy to use, supports every programming language, and is extensible and performant
100130
- [OCI GenAI](https://www.oracle.com/artificial-intelligence/generative-ai/generative-ai-service/)
101131
- Discover the power of generative AI models equipped with advanced language comprehension for building the next generation of enterprise applications. Oracle Cloud Infrastructure (OCI) Generative AI is a fully managed service for seamlessly integrating these versatile language models into a wide range of use cases, including writing assistance, summarization, analysis, and chat
132+
- [OCI Functions with GraalVM](https://github.com/shaunsmith/graalvm-fn-init-images)
133+
- Discover GraalVM Native Image -based functions with this example GitHub repo
102134
- [Oracle](https://www.oracle.com/)
103135
- Oracle Website
104136

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM container-registry.oracle.com/graalvm/native-image:23-ol8 AS native
2+
WORKDIR /app
3+
4+
COPY target .
5+
ADD reflection.json .
6+
7+
RUN native-image \
8+
-H:ReflectionConfigurationFiles=/app/reflection.json \
9+
-Ob \
10+
-H:Name=Hello \
11+
-cp "/app/Helloaifunc-1.0-SNAPSHOT.jar:/app/lib/*" \
12+
com.fnproject.fn.runtime.EntryPoint
13+
14+
FROM fnproject/fn-java-fdk:jre17-1.0.198 as fdk
15+
16+
FROM container-registry.oracle.com/os/oraclelinux:8-slim
17+
COPY --from=native /app/Hello .
18+
COPY --from=fdk /function/runtime/* .
19+
ENTRYPOINT [ "./Hello" ]
20+
CMD [ "com.example.HelloAIFunction::handleRequest", "-Djava.library.path=/lib"]

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
<scope>runtime</scope>
6868
<version>3.37.2</version>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.bouncycastle</groupId>
72+
<artifactId>bcprov-jdk18on</artifactId>
73+
<version>1.79</version>
74+
</dependency>
7075
</dependencies>
7176

7277
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "com.example.HelloAIFunction",
4+
"allDeclaredMethods": true,
5+
"methods": [
6+
{ "name": "<init>", "parameterTypes": [] }
7+
]
8+
}
9+
]
10+

app-dev/devops-and-containers/functions/java-helloworld-with-local-dev-and-oci-functions/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ docker login ams.ocir.io -u '&lt;YOUR OCI TENANCY NAMESPACE&gt;/oracleidentitycl
130130
docker push fra.ocir.io/&lt;YOUR OCI TENANCY NAMESPACE&gt;/helloworld-java:1
131131
</pre>
132132

133-
<p>
134-
135133
The last step is to create the Function Application and the function deployment for it. This can be easily done using the Cloud UI.
136134

137135
<p>

0 commit comments

Comments
 (0)