A couple of days ago, the base image for GraalVM has been released simplifying the way how to package GraalVM compatible application using…


How to Deploy Java Application with Docker and GraalVM

A couple of days ago, the base image for GraalVM has been released simplifying the way how to package GraalVM compatible application using Docker. I will use Micronaut application which supports GraalVM out of the box.

Creating a GraalVM compatible application with Micronaut is pretty simple:

# install SDKMan!
curl -s https://get.sdkman.io | bash
# use SDKMan! right ahead
source “$HOME/.sdkman/bin/sdkman-init.sh”
# install micronaut
sdk install micronaut 1.0.0.RC2
# use the installed version
sdk use micronaut 1.0.0.RC2 mn —version
# create a products application
mn create-app —features graal-native-image products
# go to the application folder
cd products
# create a product controller
mn create-controller Product

The application only exposes endpoint /product which returns 200 OK status. Now we can run ./gradlew assemble to create the fat application JAR which can be used by the Docker file. Also Micronaut creates GraalVM’s reflection configuration file for us at build/reflect.json. If you’re using other framework or no framework at all you will have to create the file yourself if required.

./gradlew assemble

It is important that GraalVM native image application is compiled on the same architecture. For that reason, it’s better if the application is compiled using Docker RUN command. Create Dockerfile with following content:

You can build and run the application using Docker using the following commands:

docker build —tag=“products-with-graavm” .
docker run -d -p 8080:8080 products-with-graavm
# now you should get OK from the running server
curl -v http://localhost:8080/product