---
title: Building small Java Docker images
url: https://blog.octoperf.com/building-small-java-docker-images/
date: '2016-02-11'
authors:
- jloisel
categories:
- DevOps
tags:
- Docker
- Java
description: Simple tutorial on how to build lightweight Docker images for Java applications.
---

# Building small Java Docker images

When you take a look at the [Java Official Docker images](https://hub.docker.com/r/library/java/tags/), 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:
```sh
FROM java:8

MAINTAINER ...
```

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

```sh
docker pull java:8
docker images | grep java
```


It should output something like:

```sh
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](https://hub.docker.com/_/alpine/) is a very small Linux distribution based on [Busybox](https://busybox.net).
It's very small, only 5MB once pulled from Docker Hub!

The [Alpine Java](https://hub.docker.com/r/anapsix/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):

```sh
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:

```sh
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:

```sh
#!/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](https://docs.docker.com/engine/articles/dockerfile_best-practices/) 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](https://projects.spring.io/spring-boot/). Why not for you?
