Working with Java inside Docker Container

Java is one of the most popular enterprise languages right now. It is the core of object oriented programming and comes with great platforms to build enterprise level applications and testing platforms. For newbies, installing and getting accommodated with the Java environment might take some time initially.

Docker Containers allow you to access Java Runtime Environment inside them, thus providing an easily manageable packaged environment with libraries already installed. If you have Docker installed on your local machine, instead of running Java applications and going through all the hustle, you can easily build a Java image by pulling it directly through the Docker registry and can run Java applications directly inside the environment provided by the Container in simple and easy steps.

In this article, we will be discussing all the steps that you need to follow to run a Java application inside the Docker Container. There are two main methods to use and run java applications inside the Docker Container.

  • You can create and build a custom image and install java using apt-get commands by specifying them separately inside the dockerfile.

  • You can directly pull the official Java image from Docker Registry and directly run your Java applications inside them.

We will be using the easier one which is the second method. Let’s go through the process step by step.

Create the build context to store the dockerfile and Java application

To begin with, you need to create a docker build context that would contain your dockerfile and the Java application.

mkdir my−java−app

Create a Java Application

You can create a simple java application in a file with .java extension. Refer the snippet below stored in a file name called “” inside the “my−java−app” directory we created in the above step

import java.utils.*;

class Main{
   public static void main(String args[]){
      System.out.println("Welcome to TutorialsPoint");

Create a Dockerfile to build the image

Create a file with the name “dockerfile”. Include the instructions below inside the file and save it inside the “my−java−app” directory which already contains the java application.

#Pull the Java base image
FROM java:8

#Set the working directory
WORKDIR /var/www/java

#Copy the build context
COPY . /var/www/java

#Compile the Java application
RUN javac

#Run the executable
CMD ["java", "Hello"]

The above dockerfile specifies all the instructions needed to create a java image and run our application inside the Docker Container associated with that image. It pulls the Java version 8 image from the Docker Registry and sets the working directory. It then copies the Docker build context which contains our Java application. It then compiles the java application using the javac command and finally runs the executable created after compilation using the CMD command.

Building the Docker Image

After you have created the dockerfile, you can use it to build your docker image using the following build command.

sudo docker build −t <image−name> .

On executing the above command, it will successfully build the Docker Image.

Run the Docker Container

You can now use the Docker run command to create a Docker Container associated with the above image and run your Java application inside the Docker Container.

sudo docker run <image−name>

To conclude, creating a perfect Java runtime environment to execute all your java applications might prove to be a hefty task. Thankfully, Docker provides pre-built Java images which you can easily pull from the Docker registry and can instantly start building your Java applications. You can also extend this to create a spring environment and all other Java enterprise platforms and can work on Java applications on a large scale. Docker Container provides a contained environment to manage all your applications without having to worry about version control, project management, resource management, etc.

In this article, we discussed how to pull the official Java image from the Docker registry, created a Dockerfile to run our Java application, use the Docker build and run commands to build the image, compile and execute the application.

Updated on: 27-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started