Saturday 25 May 2019

Docker: Run jar file from the docker image


In this post, I am going to explain how to run jar file in docker container. If you want to skip step 1, you can download the jar file ‘demoDockerApp-1.jar’ from below location.

Step 1: Generate jar file.
Open Eclipse. Create new maven project ‘demoDockerApp’.


File -> New -> Maven Project.

Select the checkbox ‘Create a simple project (skip archetype selection)’. Click on Next button.


Give Group Id and Artifact Id as 'demoDockerApp' and set the version to 1. Click on Finish button.


Total project structure looks like below.


Create a package com.sample.app.

Create Application.java like below.

Application.java
package com.sample.app;

public class Application {

 public static void main(String args[]) {
  System.out.println("Welcome to Docker Application Development");
 }
}

Update pom.xml like below.


pom.xml
<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>demoDockerApp</groupId>
 <artifactId>demoDockerApp</artifactId>
 <version>1</version>

 <build>
  <plugins>
   <plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
       <mainClass>com.sample.app.Application</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Generate jar file

Right click on the project -> Run As -> Maven install.

Generate jar file

Right click on the project -> Run As -> Maven install.


You can see the generated jar file location in console.

Step 2: Create an image with jar file.

Create a directory ‘helloJava’.

Copy the jar file ‘demoDockerApp-1.jar’ to helloJava directory.

Create ‘Dockerfile’ with below content.

Dockerfile
FROM openjdk

COPY ./demoDockerApp-1.jar /deployments/

CMD java -jar /deployments/demoDockerApp-1.jar


Execute the command 'docker image build -t myjavaapp .' to generate the image.
$ls
Dockerfile  demoDockerApp-1.jar
$
$docker image build -t myjavaapp .
Sending build context to Docker daemon   5.12kB
Step 1/3 : FROM openjdk
 ---> 831a029b6add
Step 2/3 : COPY demoDockerApp-1.jar /deployments
 ---> 313f0b383274
Step 3/3 : CMD java -jar /deployments/demoDockerApp-1.jar
 ---> Running in b1531627a67f
Removing intermediate container b1531627a67f
 ---> e7d795f59293
Successfully built e7d795f59293
Successfully tagged myjavaapp:latest

Step 3: Run the image.
$docker run myjavaapp
Welcome to Docker Application Development



Previous                                                 Next                                                 Home

2 comments:

  1. Hi, I have a number of old .jar applications that have a UI which would normally be run on Windows, will the above work too ?

    ReplyDelete