Building Small Java Docker Images

Building Small Java Docker Images

Devops   Give Your Opinion

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

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?

By - CTO.
Tags: Docker Java

Comments

 

Thank you

Your comment has been submitted and will be published once it has been approved.

OK

OOPS!

Your post has failed. Please return to the page and try again. Thank You!

OK