Skip to content

Instantly share code, notes, and snippets.

@tchxi
Forked from subhendu-de/README.md
Created April 25, 2024 18:14
Show Gist options
  • Save tchxi/73d7ed6d04d4daacd936f6d5f0e9b5cd to your computer and use it in GitHub Desktop.
Save tchxi/73d7ed6d04d4daacd936f6d5f0e9b5cd to your computer and use it in GitHub Desktop.
Deploy JSP website using Docker + Apache Tomcat from scratch(with IDEs like Eclipse)

Docker + Apache Tomcat + JSP

This article describes how to deploy a JSP website using Docker and Apache Tomcat.

Directory structure

Create a "Dynamic Web Project" in eclipse and add a jsp file index.jsp

Organize your working directory like this:

SampleWeb/
    Dockerfile
    WebContent/
        META-INF/
          MANIFEST.MF
        WEB-INF/
          classes/
          lib/
        index.jsp

Leave every folders and files empty. This is the skeleton of the Dockerized web project.

Web project

Fill some content in the jsp file. Take your favorite editor(eclipse, visual studio code etc.) and edit these files:

SampleWeb/WebContent/index.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>A JSP page in tomcat docker container</title>
</head>
<body>
This is inside the docker. The server date & time is <%= new Date().toString() %>
</body>
</html>
%>

Create a WAR file

Export the web project to the war archive. Use eclipse export feature to generate the sampleweb.war file. Please put the war file under root(SampleWeb/) directory.

And that's all for the website to demonstrate a dockerized web project.

Dockerfile

And let's make the Dockerfile. Open SampleWeb/Dockerfile and add these lines:

FROM tomcat:8.0-alpine

COPY SampleWeb.war /usr/local/tomcat/webapps/sampleweb.war

EXPOSE 8080

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

Build Docker Image and Run

Go to SampleWeb/ in terminal, and type it to build a Docker image:

$ docker build -t sampleweb:v1 .

And then run:

$ docker run -p 8081:8080 sampleweb:v1

Visit http://localhost:8081/sampleweb/index.jsp to see the website running!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment