Creating a Docker container with Alpine Linux including Java 8 and 9
Recently, I’ve decided to refresh my knowledge regarding Docker and created an image with Alpine Linux and Java 9, which can be a useful base for the future projects. I used Alpine as a base image because it became quite popular in the Docker world due to its simplicity and the fact that it’s pretty lightweight when we compare it to containers based on other Linux distributions. Pure Alpine Docker container has about 4.144 MB, what is really impressing.
Dockerfile looks pretty simple:
We’re downloading JDK, unpacking it, moving to
/usr/local/share directory, creating
$JAVA_HOME environmental variable and adding
$JAVA_HOME/bin to the
$PATH. After that, we’re removing downloaded
*.tar.gz file. We can find it on Docker Hub: https://hub.docker.com/r/pwittchen/alpine-java9/ To pull the image from Docker Hub, just type:
sudo docker pull pwittchen/alpine-java9
To run it with CLI, type:
sudo docker run -i -t pwittchen/alpine-java9
Then, we can play around with jshell inside the container:
/ # jshell
This container is not so small and has about 919.2 MB. It contains whole JDK, so probably this size could be reduced.
Right now (21st of March 2018), this solution doesn’t work any longer. Please, search for an official Alpine image with Java 9 or use image with Ubuntu and Java 9 located in dockerfiles-java repo.
I’ve also created another image with Java 8 (just in case):
sudo docker pull pwittchen/alpine-java8
and run it with CLI as follows:
sudo docker run -i -t pwittchen/alpine-java8
In this case, container has 118.5 MB, which is better result than for the previous container. In this case, we’re installing Java 8 for Alpine from official repository, so probably it’s already optimized. I hope, you’ll find it useful while developing your projects in Java 8 or Java 9.