Skip to content

olyagpl/jwebserver-website-runner

Repository files navigation

Jwebserver Running GraalVM Documentation Website

GraalVM’s documentation website served with jwebserver in a Docker container on Oracle Cloud Infrastructure.

This example demonstrates how to optimize a Java application for file size taking advantage of different Native Image containerisation and linking options. The various Dockerfiles copy a native executable or jlink generated custom runtime image folder along with static website pages into a container image, and set the entrypoint.

The jwebserver tool is a minimal HTTP server for serving static files from a single directory hierarchy, added in Java 18.

Prerequisites

Setup

  1. Clone this repository with Git:

    git clone https://github.com/olyagpl/jwebserver-website-runner.git 
  2. To complete all steps in this demo, you need the following zlib packages installed: zlib.x86_64, zlib-devel.x86_64;zlib-static.x86_64. Enter the example directory and run the following script to download and configure the musl toolchain, and install zlib into the toolchain:

    cd jwebserver-website-runner
    ./setup-musl.sh
  3. Download UPX, an advanced executable file compressor:

    ./setup-upx.sh

Create Container Images

  1. Using jlink, create a custom runtime for the application, package it into a container image that contains a JDK.

    ./build-jlink-runner.sh
  2. Build a fully dynamically linked executable and package it in a distroless container image with just enough to run the application.

    ./build-dynamic-runner.sh
  3. Build a mostly statically linked executable, by passing --static-nolibc to the native-image tool, and package it into a container image that provides glibc.

    ./build-mostly-static-runner.sh

    What can you do next to reduce the size even more?

  4. Build a fully static executable, by passing --static --libc=musl to the native-image tool, and package it into a scratch container. (It requires the musl toolchain.)

    ./build-static-runner.sh
  5. Compress the fully static executable, created at the previous step, with UPX, package it into a scratch container.

    ./build-static-compressed-runner.sh

    Note that UPX loads the native executable into the memory, unpackages it, and then compresses.

Serve the Website

Run any of the container images to serve the website, mapping the ports, for example:

docker run --rm -p8000:8000 website-runner:scratch.static

Finally, open the website in a browser at http:https://<SERVER_IP>:8080/, where the <SERVER_IP> is the public API address of the remote host. If you are running the example locally, not on a remote host, just open http:https://localhost:8000.

To stop a running container, find out the container image ID and stop it:

docker ps
docker stop <image id>

To clean up all images, run the ./clean.sh script provided for that purpose.

Container Images and Native Executables File Sizes Comparison

Compare the sizes of the various Docker container images and native executables:

ls -lh runner*
docker images website-runner

Note that the website static pages added 44M to the container images size!

Learn More

About

Native Jwebserver GraalVM Website Runner

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages