A step-by-step walkthrough of shrinking a Micronaut Java web server from a 470MB JDK-based Docker container down to a 22MB fully static image using GraalVM Native Image. The process covers seven stages: running a JAR in a distroless container (216MB), using jlink to trim the JDK (167MB), compiling ahead-of-time with GraalVM Native Image (132MB, ~20ms startup), applying the -Os size optimization flag (102MB), building a mostly-static binary with --static-nolibc (89.7MB), creating a fully static musl-linked binary in a scratch container (69.2MB), and finally applying UPX compression to reach 22.3MB. Key takeaways include the dramatic startup improvement (378ms → 20ms), the value of static linking for attack surface reduction, and the trade-offs of UPX compression.

7m read timeFrom medium.com
Post cover image
Table of contents
Step by Step: Slimming Down the ContainerGet Olga Gupalo’s stories in your inboxBefore and After: The NumbersEngineering Lessons LearnedTry It Yourself 💻

Sort: