How to dockerize a Java application?

Docker revolutionized the way applications are deployed.

To deploy an application to a server , you no more need to set up required softwares/dependencies on the server machine.

Create your application in development environment , build a docker image and then run the image on the server .

Below is a simple example of how to dockerize a java application which prints “Hello World” on the console.

STEP 1:

First create a java application .

I created using the below command:

mvn:archetype:generate -DgroupId=com.docker.java -DartifactId=dockerizejava -DinteractiveMode=false

You can also create it using eclipse or any other IDE.

STEP 2:

Package the application into a jar file.

Move to the parent directory and run the command mvn:package to generate the jar file.



Maven uses java 5 by default for packaging and applications developed using later versions of java cannot be packaged using Java 5. This can be overcome by setting maven properties for java version. Add the below entry in pom.xml (any java version greater than 5 will do)

<properties>

   <maven.compiler.source>1.8<maven.compiler.source>
   <maven.compiler.target>1.8><maven.compiler.target>
</properties>

Run the command mvn:package again and it generates a jar file (dockerizejava-1.0-SNAPSHOT.jar)

STEP 3:

Verify if the application runs .

I have used java -cp command to run the application and it returns “Hello World!”

-cp stands for classpath

Now let us dockerize the above java application.

Prerequiste : Docker is installed in your machine.

STEP 4:

Create a Dockerfile ( the file has no extension )

I created a file named ‘Dockerfile’ in the parent directory.

Inside the file I did the following:

  • Download a base image;

Docker registry contains many predefined images. This can be pulled from docker hub and our application can be added to it to create a new image. Since this is a java application I used openjdk image. Use FROM keyword to specify the base image :

FROM openjdk:latest

  • Copy the created jar file into the source folder of the base image

Copy dockerizejava-1.0-SNAPSHOT.jar into /usr/src/ directory of the image using COPY command:

COPY target/dockerizejava-1.0-SNAPSHOT.jar /usr/src/dockerizejava-1.0-SNAPSHOT.jar

  • Run the jar using CMD keyword

CMD java -cp /usr/src/dockerizejava-1.0-SNAPSHOT.jar com.docker.java.App

Here is the entire content of the file ‘Dockerfile’:

FROM openjdk:latest
COPY target/dockerizejava-1.0-SNAPSHOT.jar /usr/src/dockerizejava-1.0-SNAPSHOT.jar
CMD java -cp /usr/src/dockerizejava-1.0-SNAPSHOT.jar com.docker.java.App

STEP 5:

Once the docker file is created build the image using ‘build’ command:

docker build .

Here . specifies the current directory where ‘Dockerfile’ is placed. A name for the image can also be specified by using -t option like :

docker build -t dockerizejava .

A docker image has been created!

Now let’s run the image through the container.

STEP 6:

Run the container using the container id generated during the build. Or if an image name had been given during the build process , run it using the image name:

-it runs the container in interactive mode (you can fire new commands in the same terminal in interactive mode)

(To run with the image name use : docker run -it dockerizejava)

Hello World! got printed

That’s it !

A java application has been dockerized.

Now this can be run anywhere where docker is installed without installing a JRE.

Similarly if he had developed a complex application with many more dependencies , it can also be run anywhere where docker is installed without doing any set up!

Here is the code for the above demo:

https://github.com/vijaysrj/dockerizejava


Posted

in

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading