Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/github-actions-what-the-dickens.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Native Image What_the_Dickens Pipeline
on:
push:
paths:
- 'native-image/what-the-dickens/lab/**'
- '.github/workflows/github-actions-what-the-dickens.yml'
jobs:
build:
name: Native Image What_the_Dickens
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- uses: actions/checkout@v4
- name: Set up GraalVM
uses: graalvm/setup-graalvm@v1
with:
java-version: '24'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Test Java Code
run: |
cd native-image/what-the-dickens/lab/
# Build a JAR
./mvnw --no-transfer-progress clean package

# Generate reachability metadata with the Tracing agent
java -Dpring.aot.enabled=true \
-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/ \
-jar target/what_the_dickens-0.0.1-SNAPSHOT.jar &
pid=$!
kill $pid

# Build a regular dynamic native image and containerize in docker.io/oraclelinux:8-slim container
docker build -f ./01-native-image/Dockerfile \
-t what_the_dickens:native.01 .
container_id=$(docker run --rm -d --name "what_the_dickens-native" -p 8080:8080 what_the_dickens:native.01)
sleep 10
docker ps
curl "http://localhost:8080/whatTheDickens"
docker kill $container_id
docker ps

# Build a mostly-static native image and containerize in gcr.io/distroless/base container
docker build -f ./02-smaller-containers/Dockerfile \
-t what_the_dickens:distroless.01 .
container_id=$(docker run --rm -d --name "what_the_dickens-distroless" -p 8081:8080 what_the_dickens:distroless.01)
sleep 10
docker ps
curl "http://localhost:8081/whatTheDickens"
docker kill $container_id
docker ps

# List container images
docker images

# Build a native image
./mvnw --no-transfer-progress -Pnative -DskipTests=true package
3 changes: 0 additions & 3 deletions native-image/what-the-dickens/.vscode/settings.json

This file was deleted.

576 changes: 242 additions & 334 deletions native-image/what-the-dickens/README.md

Large diffs are not rendered by default.

Binary file not shown.
Binary file removed native-image/what-the-dickens/images/keyboard.jpg
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.5/apache-maven-3.8.5-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
18 changes: 5 additions & 13 deletions native-image/what-the-dickens/lab/01-native-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Base Container Image
FROM ghcr.io/graalvm/native-image:ol8-java17-22.3.1 AS builder

# Install tar and gzip to extract the Maven binaries
RUN microdnf update \
&& microdnf install --nodocs \
tar \
gzip \
&& microdnf clean all \
&& rm -rf /var/cache/yum
FROM container-registry.oracle.com/graalvm/native-image:24 AS builder

# Set the working directory to /build
WORKDIR /build
Expand All @@ -19,13 +11,13 @@ COPY . /build
RUN ./mvnw --no-transfer-progress -Pnative -DskipTests=true clean package

# The deployment container image
FROM docker.io/oraclelinux:8-slim
FROM gcr.io/distroless/java-base-debian12

# This container will expose TCP port 8080, as this is the port on which your app will listen
EXPOSE 8080

# Copy the native executable into the container
COPY --from=builder /build/target/What_the_Dickens .
COPY --from=builder /build/target/what_the_dickens .

# Run What_the_Dickens when starting the container
ENTRYPOINT ["/What_the_Dickens"]
# Run what_the_dickens when starting the container
ENTRYPOINT ["/what_the_dickens"]
21 changes: 6 additions & 15 deletions native-image/what-the-dickens/lab/02-smaller-containers/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Base Container Image
FROM ghcr.io/graalvm/native-image:ol8-java17-22.3.1 AS builder

# Install tar and gzip to extract the Maven binaries
RUN microdnf update \
&& microdnf install --nodocs \
tar \
gzip \
&& microdnf clean all \
&& rm -rf /var/cache/yum
FROM container-registry.oracle.com/graalvm/native-image:24 AS builder

# Set the working directory to /build
WORKDIR /build
Expand All @@ -16,17 +8,16 @@ WORKDIR /build
COPY . /build

# Build
RUN ./mvnw --no-transfer-progress -Pnative -DskipTests=true clean package
RUN ./mvnw --no-transfer-progress -Pnative,mostly-static -DskipTests=true clean package

# Deployment Container
# This time we use the distroless image - which is around 20 MB in size. Even smaller versions are available.
FROM gcr.io/distroless/base
FROM gcr.io/distroless/base-debian12

# This container will expose TCP port 8080, as this is the port on which your app will listen
EXPOSE 8080

# Copy the native executable into the container
COPY --from=builder /build/target/What_the_Dickens .
COPY --from=builder /build/target/what_the_dickens .

# Run What_the_Dickens when starting the container
ENTRYPOINT ["/What_the_Dickens"]
# Run what_the_dickens when starting the container
ENTRYPOINT ["/what_the_dickens"]
63 changes: 25 additions & 38 deletions native-image/what-the-dickens/lab/HELP.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,49 @@
# Getting Started

### Reference Documentation
For further reference, please consider the following sections:

* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/3.0.5/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/3.0.5/maven-plugin/reference/html/#build-image)
* [GraalVM Native Image Support](https://docs.spring.io/spring-boot/docs/3.0.5/reference/html/native-image.html#native-image)

### Additional Links
These additional references should also help you:

* [Configure AOT settings in Build Plugin](https://docs.spring.io/spring-boot/docs/3.0.5/maven-plugin/reference/htmlsingle/#aot)

## GraalVM Native Support
# Help Guide

This project has been configured to let you generate either a lightweight container or a native executable.
It is also possible to run your tests in a native image.
It is also possible to run your tests as a native code.

### Lightweight Container with Paketo Buildpacks

### Lightweight Container with Cloud Native Buildpacks
If you're already familiar with Spring Boot container images support, this is the easiest way to get started.
Docker should be installed and configured on your machine prior to creating the image.
Docker should be installed and configured on your machine prior to running the steps.

To create the image, run the following goal:

```
$ ./mvnw spring-boot:build-image -Pnative
```bash
./mvnw spring-boot:build-image -Pnative
```

Then, you can run the app like any other container:

```
$ docker run --rm What_the_Dickens:0.0.1-SNAPSHOT
```bash
docker run --rm what_the_dickens:0.0.1-SNAPSHOT
```

### Executable with Native Build Tools
Use this option if you want to explore more options such as running your tests in a native image.
The GraalVM `native-image` compiler should be installed and configured on your machine.

NOTE: GraalVM 22.3+ is required.
Use this option if you want to explore more options such as running your tests as a native code.
[GraalVM for JDK 24](https://www.graalvm.org/downloads/) should be installed on your machine.

To create the executable, run the following goal:

```
$ ./mvnw native:compile -Pnative
```bash
./mvnw native:compile -Pnative
```

Then, you can run the app as follows:
```
$ target/What_the_Dickens
Then, you can run the application as follows:
```bash
./target/what_the_dickens
```

You can also run your existing tests suite in a native image.
You can also run your existing tests suite as a native code.
This is an efficient way to validate the compatibility of your application.

To run your existing tests in a native image, run the following goal:

```
$ ./mvnw test -PnativeTest
```bash
./mvnw test -PnativeTest
```

### Learn More

For further reference, check the following:

* [Build a Native Executable from a Spring Boot Application](https://www.graalvm.org/latest/reference-manual/native-image/guides/build-spring-boot-app-into-native-executable/)
* [Developing Your First GraalVM Native Application](https://docs.spring.io/spring-boot/how-to/native-image/developing-your-first-application.html)
Loading