This article describes how to deploy a JSP website using Docker and Apache Tomcat.
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.
Fill some content in the jsp file. Take your favorite editor(eclipse, visual studio code etc.) and edit these files:
<%@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>
%>
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.
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"]
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!