Skip to content
Building small Java Docker images

Building small Java Docker images

When you take a look at the Java Official Docker images, you will immediately notice how fat they are: a whopping 243MB announced for a simple Java 8 JDK base image! It takes forever to download them from a regular DSL connection here.

Usually, your Dockerfile for Java apps starts with something like:

FROM java:8

MAINTAINER ...

Just try the following command: (you must have docker installed)

docker pull java:8
docker images | grep java
Need professional services?
Rely on our Expertise

It should output something like:

java   8   d4849089125b   2 weeks ago   642 MB

Wow! 643MB just to be able to launch a Java application inside a container. And you don't even have bundled your application yet!

Alpine Linux

Alpine Linux is a very small Linux distribution based on Busybox. It's very small, only 5MB once pulled from Docker Hub!

The Alpine Java is of course bigger but way smaller than Java official's one. This is the size of the latest Alpine Java using Oracle 8 JRE (Server JRE):

anapsix/alpine-java             latest              3bc26369bc7d        13 days ago         177.5 MB

That's almost 500MB smaller than the official one! Here the docker file we use to run our spring boot powered backend:

FROM anapsix/alpine-java

MAINTAINER Jerome Loisel

ADD target/app.jar app.jar
ADD entrypoint.sh /entrypoint.sh

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]

And the entrypoint.sh script:

#!/bin/bash

set -e

JAVA_OPTS=${JAVA_OPTS:="-Xmx256m"}

exec java -jar $JAVA_OPTS /app.jar

Lighter docker images are essential when deploying your app frequently. The bigger the image is, the more time it takes for Docker to download and extract it.

Other Tips

Take a look at Best practices for writing Docker images on the Docker website. It contains many tips and tricks to reduce your docker images size.

Conclusion

It seems so easy, it's almost magic. It works great for our backend written with Java 8 and Spring Boot. Why not for you?

Want to become a super load tester?
Request a Demo