Java with docker

Submitted on Thu, 08/15/2024 - 16:12

Tags

Dockerize a Java web app from a war file.

Filename: wolves.war

Create a Dockerfile in the same folder as follows:

FROM tomcat:9-jdk11

COPY wolves.war /usr/local/tomcat/webapps/ROOT.war

EXPOSE 8080

CMD ["catalina.sh","run"]

 

Build a Docker image and run a container

docker build -t javawar .

docker run -dit -p 8080:8080 –name warserv javawar

 

To access the web application read the content of a file called web.xml at /usr/local/tomcat/webapps/ROOT/WEB-INF in the docker container.

 

web.xml

<web-app>
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>org.apurwa.Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

The URL mapped is /hello thus our access path is:

localhost:8080/hello


Dockerize a Java standalone app from a jar file.

Filename: Hello.jar

 

Create a Dockerfile as follows:

From openjdk:17-jre-slim

WORKDIR /app

COPY Hello.jar .

CMD ["java", "-jar", "Hello.jar"]

 

Build a Docker image and run a container

docker build -t javajars .

docker run –rm javajars


Dockerize a java app from a single java file

Hello.java

public class Hello{

public static void main(String[] args){

     System.out.println("Java sucks.");

}

}

 

Create a Dockerfile as follows in the same folder:

FROM openjdk:11-jdk-slim

WORKDIR /app

COPY Hello.java /app

RUN javac Hello.java

CMD ["java","Hello"]

 

Build Docker image and run the Docker container

docker build -t standjava .

docker run --name standserv standjava


Dockerize a java web app from source code using WAR file

Source code

File

Path

Hello.java

src/main/java/org/apurwa

web.xml

src/main/webapp/WEB-INF

pom.xml

Project root folder

 

Hello.java

package org.apurwa;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Hello extends HttpServlet{

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

response.setContentType("text/html");

response.getWriter().println("<h1>Java sucks!</h1>");

}

}

 

web.xml

<web-app>

<servlet>

<servlet-name>Hello</servlet-name>

<servlet-class>org.apurwa.Hello</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Hello</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

</web-app>

 

pom.xml

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>org.apurwa</groupId>

<artifactId>webjava</artifactId>

<packaging>war</packaging>

<version>1</version>

<dependencies>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>4.0.1</version>

<scope>provided</scope>

             </dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>11</source>

<target>11</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>

<version>3.3.2</version>

<configuration>

<warName>ROOT</warName>

</configuration>

</plugin>

</plugins>

</build>

</project>

 

Create a Dockerfile in the root directory as follows:

FROM maven:3.8.4-openjdk-11 AS builder

WORKDIR /app

COPY pom.xml .

COPY src ./src

RUN mvn clean package

FROM tomcat:9.0-jdk11-openjdk-slim

COPY --from=builder /app/target/ROOT.war /usr/local/tomcat/webapps/

EXPOSE 8080

CMD ["catalina.sh", "run"]

 

Build the Docker image and run a container:

docker build -t javawar .

docker run -dit -p 5000:8080 --name webjserv javawar

 

The war file is called wolves and the servlet is mapped to /hello. Thus the access path is:

http://localhost:5000/hello