Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Last active February 1, 2024 09:15
Show Gist options
  • Save jdrew1303/ddca82814b3cf466fd77ff8a08e3134b to your computer and use it in GitHub Desktop.
Save jdrew1303/ddca82814b3cf466fd77ff8a08e3134b to your computer and use it in GitHub Desktop.
Distroless GraalVM Dropwizard

This is meant to be a tester for running compiling Dropwizard to a native app using GraalVM and then deploying it using docker and Google's Distroless containers. This means that our application startup time should be orders of magnitude faster and our application size (and attack/scan surface area) should be as small as is possible by todays standards.

Another way to make this smaller again is by running docker on top of alpine linux. This is probably as tiny as you can go.

NOTE:

  • reflect.yml will need to be added to as docker has dependencies that use reflection heavily. This will depend on your use case but something to be wary of (in other words dont try this with Spring :P ). reflect.yml needs to be in a folder in src/main/docker/
  • This is a work in progress. IT MAY NOT WORK (still in testing)
package com.jdrew1303.tinywizard;
import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
public class DemoApplication extends Application<DemoConfiguration> {
public static void main(String[] args) throws Exception {
new DemoApplication().run(args);
}
@Override
public void run(DemoConfiguration config, Environment env) throws Exception {
ExampleApi api = new ExampleApi();
environment.jersey().register(api);
}
}
package com.jdrew1303.tinywizard;
import io.dropwizard.Configuration;
public class DemoConfiguration extends Configuration {
}
FROM maven:3.6.0-jdk-8-alpine as builder
COPY . /root/app/
WORKDIR /root/app
RUN mvn dependency:resolve
RUN mvn install
FROM oracle/graalvm-ce:1.0.0-rc14 as graalvm
COPY --from=builder /root/app/ /home/app/
WORKDIR /home/app
RUN native-image --no-server \
--class-path target/hello-world.jar \
-H:EnableURLProtocols=http \
-H:Name=hello-world \
-H:Class=com.jdrew1303.tinywizard.DemoApplication \
--allow-incomplete-classpath
# this is a special container for graalvm containing zlib, glibc,
# libssl and openssl. these are used quite a bit by java applications
# (and graal applications).
FROM FROM cescoffier/native-base:latest
EXPOSE 8080
COPY --from=graalvm /application
ENTRYPOINT ["./application"]
package com.jdrew1303.tinywizard;
import com.codahale.metrics.annotation.Timed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
public class ExampleApi {
@GET
@Timed
public String hello() {
return "Boom! It's a tiny wizard app! :) ";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jdrew1303</groupId>
<artifactId>dropwizard-graalvm-native-distroless</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dropwizard.version>1.3.12</dropwizard.version>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hello-world</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.jdrew1303.tinywizard.DemoApplication</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
[
{
"name" : "io.dropwizard.jetty.ConnectorFactory",
"allDeclaredConstructors" : true
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment