It's about time to learn how to build your own OpenJDK 8 Image and learn how to install Java properly into Docker.

We will discuss a few very important details in building your own Java Image to be used in your developer operations pipeline.

You may learn to write Java during College courses or even learn to write Java while on the Job.

Java is a high-level/class-based object-oriented programming language that is designed to have as few implementation dependencies as possible.

Java is one of the most used programming language in the united states with over 126,574 java developers currently employed.

In this article we will discuss all the requirements and prerequisites you will need to run Open JDK 8 inside docker.

If you find this helpful feel free to give it a follow at the bottom of the article! Enjoy the tutorial!

Let's start by creating a base image in docker with Ubuntu.

My go-to ubuntu version is 16.04.

Let's write a Dockerfile that can use this version of the Ubuntu to give ourselves a head start in setting up Java properly.

FROM ubuntu:16.04

Then let's evaluate the rest of the requirements. We first want to include the installer for OpenJDK. Many companies run on OpenJDK 8 so let's start with version 8 of Java. You can always upgrade later.

Next write these commands into the Dockerfile.

RUN apt-get install -y openjdk-8-jdk

Make sure you have setup Ubuntu to be preconfigured as a Non-interactive user. We will write a few more commands above this to ensure we have all the required configurations for Ubuntu to install Java JDK properly.

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

If these settings did not exist before install java, you would see errors while compiling the docker container. Make sure to include these settings before running apt-get.

# Install Ubuntu Prerequisites
RUN apt-get update -y && apt-get install -y --no-install-recommends apt-utils

Great! Now OpenJDK8 is available inside your docker container.

However you may have noticed the OpenJDK8 installer is not yet finished because the Java environment namespace is actually non-existent.

Let's correct our problem from before and setup our Java executable to be accessible from anywhere inside the docker container while running.

ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
ENV PATH=$PATH:$JAVA_HOME/bin

Perfect! Now we can accessible the path to our Java-8–openjdk-amd64 executable.

Once our Ubuntu instance has the Java executable available we can then remove the unnecessary installed source packages to help clean up the memory of the docker container.

Go ahead and simply add the following command to the bottom of your Dockerfile.

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

Awesome! We have cleaned up our Ubuntu Instance. Next let's test out the running OpenJDK8 Java install.

CMD ["java", "--version"]

Now we simply need to build and run the Dockefile!

docker build -t openjdkjava8 . && docker run openjdkjava8:latest

If you need help running a container as a beginner and are would like to start using Dockerfile visit our cheat-sheet for more tips!

Happy Programming! ♨️

None

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week