Skip to content

Instantly share code, notes, and snippets.

@hallazzang
Created November 30, 2017 07:43
Show Gist options
  • Save hallazzang/c346e544b1c6fce8f304fdd5b2295fb6 to your computer and use it in GitHub Desktop.
Save hallazzang/c346e544b1c6fce8f304fdd5b2295fb6 to your computer and use it in GitHub Desktop.
Deploy JSP website using Docker + Apache Tomcat from scratch(without IDEs like Eclipse, IntelliJ, etc.)

Docker + Apache Tomcat + JSP

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

Directory structure

I found a way to make a minimal JSP web application from scratch, without any IDE. (ref: https://www.youtube.com/watch?v=JEBR_KJdzSk)

First, organize your working directory like this:

yourproject/
    Dockerfile
    webapp/
        WEB-INF/
          classes/
          lib/
          web.xml
        index.jsp

Right now, just leave every folders and files empty. This is the skeleton of our Dockerized web application.

Web application

First, we should fill some content for our web application. Take your favorite editor and edit these files:

yourproject/webapp/WEB-INF/web.xml

<web-app>
</web-app>

It can be empty. Later, you can edit this file as you want.

yourproject/webapp/index.jsp

<!doctype html>
<h1>It works!</h1>
<%
  for (int i = 0; i < 5; ++i) {
      out.println("<p>Hello, world!</p>");
  }
%>

This is just an example; it will produce:

<h1>It works!</h1>
<p>Hello, world!</p><p>Hello, world!</p><p>Hello, world!</p><p>Hello, world!</p><p>Hello, world!</p>

And that's all for our website. Not too complicated, right?

Dockerfile

And let's make our Dockerfile. Open yourproject/Dockerfile and copy&paste these:

FROM tomcat:9.0.1-jre8-alpine

ADD ./webapp /usr/local/tomcat/webapps/webapp

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

That's everything we need. Let me explain each line:

Apache Tomcat Image

FROM tomcat:9.0.1-jre8-alpine

We use official tomcat:9.0.1-jre8-alpine image for our base image. You can choose any other images you want, like tomcat:8.5.23-jre8. But tomcat:9.0.1-jre8-alpine is the latest release of Apache Tomcat image on Docker Hub now and has much smaller(~113MB) size than tomcat:9.0.1-jre8(~557MB) so I'll stick to use it.

Copying Website and Start up Tomcat

ADD ./webapp /usr/local/tomcat/webapps/webapp

The ADD instruction copies our local files(in our case, yourproject/webapp folder) to container's file system(/usr/local/tomcat/webapps/webapp). I tried to use a WAR file instead of plain source codes, but it didn't work for me for some reason. If someone knows how to pre-compile all the JSP files and copy it into image, please leave a comment.

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

Finally, the CMD instruction will start up Apache Tomcat, which runs our web application.

Build Docker Image and Run

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

$ docker build -t mywebapp .

And then run:

$ docker run --rm -it -p 8888:8080 mywebapp

Visit http://localhost:8888/webapp to see if your website is running!

@jpwilliams000
Copy link

Hey, I have been working on the same type of project this week. Have you had any success or new updates on your progress?

@kadirsahan
Copy link

Nice job , thanks.

@liuweiming1997
Copy link

while using tomcat in docker, it may be very slow to finish deploy(may be five minutes), cause the default jre config use /dev/random

like this

so should add RUN sed -i 's/file:\/dev\/random/file:\/dev\/urandom/' /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/java.security in dockerfile

@granadacoder
Copy link

Everything worked for me....~except the last url ("http://localhost:8888/webapp"). Using this video ( https://www.youtube.com/watch?v=yOudtpXDPzw ) I figured out that docker was actually hosting my app at a different local IP. Mine happen to be http://192.168.99.100:8888/webapp/ But you'll need to look at the video at 2:50 time mark to see how that person shows the (alternate) IP address. THANKS FOR THIS DEMO !

@KUNTALDEB
Copy link

KUNTALDEB commented Jan 9, 2019

Ok everything is fine ...but in the Docker file where is the command to install JDK inside the docker container..in the docker file only Tomcat image is pulled

@hallazzang
Copy link
Author

hallazzang commented Jan 27, 2019

Sorry for late feedback. Today I'm just surprised that no single notification from Gist has come through GitHub notification center.
@jpwilliams000 I haven't used JSP for long time, actually I used JSP for that time only, thus there was no update on my progress.
@liuweiming1997 Thank you for pointing out that. I thought the slowness was natural thing in JSP&Tomcat ecosystem.
@granadacoder Thank you too, I didn't realised that on my machine.
@KUNTALDEB Well that tomcat image is based on openjdk image, I think the necessary steps are automatically done when you just use tomcat image. See https://github.com/docker-library/tomcat/blob/master/9.0/jre8-alpine/Dockerfile#L1 for the details.

@subhendu-de
Copy link

I tried to create a web project and able to run the project from docker container. Please find the details here. Thanks @hallazzang for the gist to fork and update accordingly!

@alexandrellemes
Copy link

Top.

@Kushal-Nagwanshi
Copy link

This was really helpful, thank a lot !

@stephmafole
Copy link

Thank you very much !!!

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