Dockerizing a Java Application: A Step-by-Step Guide

Dockerizing your Java application can provide numerous benefits, including consistent deployment environments, ease of scaling, and simplified dependency management. This guide will walk you through the process of creating a Docker image for your Java application.

Prerequisites

  • JDK 17 or later
  • Maven or Gradle
  • Docker installed on your machine
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a Java Application

For this tutorial, we'll create a simple Java application using Maven. If you already have a Java application, you can skip this step.

1.1 Create a New Maven Project

You can use your IDE to create a new Maven project or use the command line.

mvn archetype:generate -DgroupId=com.example -DartifactId=my-java-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd my-java-app

1.2 Create a Simple Java Application

Edit the App.java file in the src/main/java/com/example directory.

package com.example;

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

1.3 Update pom.xml

Ensure that your pom.xml is configured correctly. The default configuration provided by the Maven archetype should suffice.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-java-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.example.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Explanation:

  • maven-compiler-plugin: Configures the Java compiler.
  • maven-assembly-plugin: Packages the application into a jar with dependencies.

Step 2: Build the Java Application

Run the following command to build the jar file of your Java application:

mvn clean package

This command will generate a jar file in the target directory, for example, target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar.

Step 3: Create a Dockerfile

Create a file named Dockerfile in the root directory of your project. This file will contain instructions for building the Docker image.

# Use the official OpenJDK base image
FROM openjdk:17-jdk-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy the built jar file into the container
COPY target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar app.jar

# Expose port 8080 (if your application runs on a specific port)
EXPOSE 8080

# Run the application
CMD ["java", "-jar", "app.jar"]

Explanation:

  • FROM openjdk:17-jdk-alpine: Uses the official OpenJDK base image with JDK 17.
  • WORKDIR /app: Sets the working directory inside the container to /app.
  • COPY target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar app.jar: Copies the built jar file to the container.
  • EXPOSE 8080: Exposes port 8080 (modify if your application uses a different port).
  • CMD ["java", "-jar", "app.jar"]: Runs the application.

Step 4: Build the Docker Image

Run the following command to build the Docker image:

docker build -t my-java-app .

Explanation:

  • docker build: The Docker command to build an image.
  • -t my-java-app: Tags the image with the name my-java-app.
  • .: Specifies the current directory as the build context.

Step 5: Run the Docker Container

Run the following command to start a Docker container from the image:

docker run -p 8080:8080 my-java-app

Explanation:

  • docker run: The Docker command to run a container.
  • -p 8080:8080: Maps port 8080 of the container to port 8080 on the host machine.
  • my-java-app: The name of the Docker image to run.

Step 6: Verify the Application

Open a web browser or a tool like Postman and navigate to the following URL (if your application has a web interface):

http://localhost:8080

For this tutorial, since the application prints "Hello, Docker!" to the console, you can check the console output to see the message.

Additional Docker Commands

6.1 List Docker Images

To list all Docker images on your system, run:

docker images

6.2 List Running Containers

To list all running Docker containers, run:

docker ps

6.3 Stop a Running Container

To stop a running Docker container, run:

docker stop <container_id>

Replace <container_id> with the actual container ID obtained from the docker ps command.

6.4 Remove a Docker Container

To remove a Docker container, run:

docker rm <container_id>

Replace <container_id> with the actual container ID.

6.5 Remove a Docker Image

To remove a Docker image, run:

docker rmi my-java-app

Conclusion

In this guide, you have learned how to dockerize a Java application. We covered:

  • Setting up a simple Java application.
  • Writing a Dockerfile to build and run the Java application.
  • Building the Docker image.
  • Running the Docker container.
  • Verifying the application.

By following these steps, you can easily package your Java application into a Docker container, providing a consistent and portable deployment environment.

Comments