Containerize LEADTOOLS Applications with Docker and Maven - Java

This tutorial shows how to set up a Java Console application with Maven using the LEADTOOLS SDK in a Docker container.

Overview  
Summary This tutorial covers how to Containerize a Java Console Application using LEADTOOLS with Maven and Docker.
Completion Time 30 minutes
Platform Java Console Application with Maven using Docker
IDE Eclipse
Development License Download LEADTOOLS

Required Knowledge

Before working on the Containerize LEADTOOLS Applications with Docker and Maven - Java tutorial, ensure that you install the LEADTOOLS SDK as well as Docker for Desktop. If you do not have both downloaded, use the below links to access the respective installers.

Prepare Project for use with Docker

Note

Since the Java project is using Maven, be sure to move your package and main class under \src\main\java\<PROJECT_NAME>\Main.java, or else Maven will not be able to locate it upon execution in the Docker container.

Create a folder called lead in the same directory that contains the Java project's src folder. Inside the lead folder create another folder called src. Add all the LEADTOOLS JAR files required by the application to this new src folder. Create a new file called pom.xml in the same src directory.

Add the following code to the pom.xml file.

<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>org.leadtools</groupId> 
    <artifactId>App</artifactId> 
    <version>1.0</version> 

Add the below code to the pom.xml file to determine version variables for later code and set the compiler version needed for proper building.

    <properties> 
        <version.maven-install-plugin>3.0.0-M1</version.maven-install-plugin> 
        <version.leadtools>21.0.0.1</version.leadtools> 
        <maven.compiler.source>15</maven.compiler.source> 
        <maven.compiler.target>15</maven.compiler.target> 
    </properties> 

Next, you need to define each of the LEADTOOLS JAR files added to the program as a dependency. Notice ${version.leadtools}, this pulls the value from the variable we set above. A <dependency></dependency> block and <execution></execution> block will need to be added for each LEADTOOLS JAR in the project.

    <dependencies> 
        <dependency> 
            <groupId>com.leadtools</groupId> 
            <artifactId>leadtools</artifactId> 
            <version>${version.leadtools}</version> 
        </dependency> 
    </dependencies> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-install-plugin</artifactId> 
                <version>${version.maven-install-plugin}</version> 
                <executions> 
                    <!-- Leadtools Jar --> 
                    <execution> 
                        <id>install-external-leadtools-jar</id> 
                        <phase>initialize</phase> 
                        <goals> 
                            <goal>install-file</goal> 
                        </goals> 
                        <configuration> 
                            <groupId>com.leadtools</groupId> 
                            <artifactId>leadtools</artifactId> 
                            <version>${version.leadtools}</version> 
                            <file>${project.basedir}/lead/src/leadtools.jar</file> 
                            <packaging>jar</packaging> 
                            <generatePom>true</generatePom> 
                        </configuration> 
                    </execution> 
                </executions> 
            </plugin> 

This final section instructs Maven to make the generated JAR file runnable so docker can execute it. Change the <mainClass></mainClass> block to match the program's package name and main class.

            <!-- Builds Runnable Application Jar --> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-assembly-plugin</artifactId> 
                <executions> 
                    <execution> 
                        <phase>package</phase> 
                        <goals> 
                            <goal>single</goal> 
                        </goals> 
                        <configuration> 
                            <archive> 
                                <manifest> 
                                    <addClasspath>true</addClasspath> 
                                    <mainClass>set_license_maven.Main</mainClass> 
                                </manifest> 
                            </archive> 
                            <descriptorRefs> 
                                <descriptorRef>jar-with-dependencies</descriptorRef> 
                            </descriptorRefs> 
                        </configuration> 
                    </execution> 
                </executions> 
            </plugin> 
        </plugins> 
    </build> 
</project> 

Create the Docker File

In the same directory as the pom.xml file, create a new file named Dockerfile. This file will contain all the instructions for Docker to build the container. The Dockerfile instructs Docker to install the required dependencies within the container so that everything functions correctly.

Setup Project Files

This section of the Dockerfile will setup the project files to run in the container. First, it installs the SDK required by the application (change to match accordingly, information found at hub.docker.com/_/debian).

Next it sets the container's current directory to /app and copies the files needed by Maven in the local target directory to the containers app directory. Run Maven to build the JAR File.

### Building the project (https://hub.docker.com/_/debian) 
 
FROM maven:3.6-openjdk-17-slim AS build-env 
WORKDIR /app 
 
# Copy files needed by Maven to container 
COPY /src /app/src 
COPY /lead /app/lead 
COPY pom.xml /app/pom.xml 
 
# Build JAR File 
RUN mvn initialize 
RUN mvn -f /app/pom.xml clean install 

Setup Runtime Environment

This next section of the Dockerfile will setup the runtime environment in the container.

First, it installs the runtime required by the application (change to match accordingly, info found at hub.docker.com/_/debian) and sets the container's current directory to Temp. It then sets a container environment DEBIAN_FRONTEND to non-interactive to prevent any confirmations or optional inputs to be requested by installing dependencies. Finally, it updates the apt-get and installs the apt-utils and wget to further facilitate installing the other dependencies.

### Building the runtime image (hub.docker.com/_microsoft-dotnet-runtime) 
 
FROM debian:sid 
WORKDIR /temp 
 
# Generate the APT cache 
 
ENV DEBIAN_FRONTEND noninteractive 
RUN apt-get update \ 
    && apt-get install -y apt-utils wget 

Install Java and Set Environment Variables

In order to run Java applications in this Docker container, we need to install Java and set Environment Variables to access Java commands.

Use wget to instruct the container to download the JDK required for installation (adjust link to match the JDK your application is made in). Use apt-get to instruct the container to install the files to it.

The ENV lines set the environment variables to tell the container how to use Java commands. The RUN command confirms Java is installed and the container understands Java commands and updates the apt-get.

# Install Java 
 
RUN wget https://download.java.net/openjdk/jdk14/ri/openjdk-14+36_linux-x64_bin.tar.gz 
RUN apt-get install -y --no-install-recommends openjdk-15-jdk 
ENV JAVA_HOME=/usr/lib/jvm/openjdk-15-jdk 
ENV PATH=$PATH:$JAVA_HOME/bin 
RUN java --version 
 
# Update APT cache 
 
RUN apt-get update 

Setup LEADTOOLS Dependencies

This section installs the dependencies needed by LEADTOOLS in order to run in the container.

The Microsoft fonts section is only needed for applications with a GUI. The wkhtmltopdf section is needed to process HTML and provide dependencies in license processing.

# Install the LEAD dependencies (https://www.leadtools.com/help/sdk/v21/main/api/getting-started-with-the-leadtools-linux-libraries-and-demo-projects.html) 
 
RUN apt-get install -y \ 
    uuid-dev uuid-runtime gcc g++ libc-dev-bin \ 
    linux-libc-dev libx11-6 libx11-dev libxt6 \ 
    libxt-dev sqlite3 libsqlite3-dev libfreetype6 
 
# Install Microsoft fonts (http://askubuntu.com/a/25614) 
 
#RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections 
#RUN apt-get install -y \ 
#    fontconfig ttf-mscorefonts-installer 
 
# Install wkhtmltopdf (http://wkhtmltopdf.org) 
RUN wget -q https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb 
RUN apt install -y --allow-unauthenticated \ 
   ./wkhtmltox_0.12.6-1.buster_amd64.deb 
RUN ldconfig 

Final Setup

In this final section we do final clean up, final setup, and specify how to initialize the application.

Set the containers current directory to app and remove the Temp directory we made as it is no longer needed. If your application is a web-based application, uncomment out EXPOSE 80 so your application will listen on Port 80, change port number as needed. Next, the contents of the app directory in the container that was stored in the buid-env section is pasted to the app folder. Lastly, we set the command java -jar app.jar to run when we start the container which triggers the application to run.

# Delete the temp files 
 
WORKDIR /app 
RUN rm -rf /temp 
 
#EXPOSE 80 
 
# Copy and deploy application 
 
COPY --from=build-env /app ./ 
COPY --from=build-env /app/target/App-1.0-jar-with-dependencies.jar ./app.jar 
ENTRYPOINT ["java", "-jar", "app.jar"] 

Run the Project

Build the Docker Container

Open the Command Prompt/Terminal or Windows PowerShell and navigate to your project folder that contains the Dockerfile using the below command.

cd '<PathToProject>' 

Build and name the container <project_name> all in lower case format and single word using the script below:

docker build -t <project_name> . 

It may take a few minutes for Docker to complete. Once completed, the image can then be run.

Run the Docker Container

Once the Docker image is built, use the following command to run the container:

docker run <project_name> 

Docker Run Commands

If your application needs user input, you need to run this command to allow interaction:

docker run -i <project_name> 

If your application needs import/export files, you can direct it to a local directory via this command:

docker run -v /c/temp:/app/data <project_name> 

This will map the local C:\Temp folder to the container's /app/data folder.

Wrap-up

This tutorial showed how to containerize a Java application in a Docker Container.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.