In modern software development, containerization has become a crucial practice for packaging and deploying applications. Docker, a leading containerization platform, allows developers to create isolated environments to run applications consistently across different systems. Java, a widely-used programming language, can greatly benefit from Docker’s capabilities by packaging Java applications into containers, simplifying deployment and scaling.

This guide walks you through the process of creating a JAR file for your Java application and then Dockerizing it.

Prerequisites

  • A server running Ubuntu with Docker installed.
  • A root user or a user with sudo privileges.

Step 1 – Create a JAR File

To package a Java application into a JAR file, you need to follow these steps:

1. Create your Java application. For example, let’s say you have a simple application with a main class HelloDocker.java:

nano HelloDocker.java

Add the following code.

package com.example;

public class HelloDocker {
    public static void main(String[] args) {
        System.out.println("Hello, Docker!");
    }
}

2. Create a manifest file to specify the main class of the application.

nano MANIFEST.MF

Add the following lines:

Manifest-Version: 1.0
Main-Class: com.example.HelloDocker

3. Compile your Java application using the javac command. This will generate .class files in the out directory.

javac -d out HelloDocker.java

4. Use the jar command to package the compiled classes into a JAR file. For example, to create a JAR file named app.jar:

jar cfm app.jar MANIFEST.MF -C out/ .

This command creates a JAR file app.jar containing all the classes in the out directory.

Step 2 – Dockerizing the Java Application

To Dockerize the Java application, you’ll need to create a Dockerfile, build a Docker image, and run a container.

1. Create a Dockerfile in the same directory as your JAR file. This file defines the environment and the commands to run your Java application in a Docker container.

nano Dockerfile

Add the following configuration.

# Use a base image with Java installed
FROM openjdk:11-jre-slim

# Set the working directory
WORKDIR /app

# Copy the JAR file into the container
COPY app.jar app.jar

# Command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]

2. Build the Docker image using the docker build command. Run this command in the directory containing your Dockerfile:

docker build -t my-java-app .

This command builds a Docker image named my-java-app. You can verify it using the following command.

docker images

Output.

REPOSITORY    TAG           IMAGE ID       CREATED          SIZE
my-java-app   latest        e4d514a07674   14 seconds ago   223MB
openjdk       11-jre-slim   764a04af3eff   24 months ago    223MB

3. Run the Docker container using the docker run command:

docker run -d --name my-running-app -p 8080:8080 my-java-app

To view the logs generated by your Java application, you can use the docker logs command.

docker logs -f my-running-app

You can see the “Hello, Docker!” message.

Hello, Docker!

Conclusion

Dockerizing a Java application provides a streamlined and efficient way to deploy and manage software in various environments. By encapsulating the application, its dependencies, and runtime configurations into a Docker container, you achieve a consistent and portable deployment process. Try to create your own Java application with Docker on dedicated server hosting from Atlantic.Net!