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

But! What is meant by JSP?

JSP is the acronym for Java Server Pages, a language created by SUN free of charge, JSP is an open specification scripting language whose primary purpose is to generate dynamic content for web pages. We can randomly use HTML to develop static web pages without functionality, JSP to create dynamism. It is possible to write HTML with embedded JSP codes.

As HTML is a static language, JSP will be responsible for creating dynamism. As it is free and has completely open-source, it has several servers that support a language, among them, we have Tomcat, GlassFish, JBoss, among others. The JSP requires the server to function as it is a server-side scripting language, the user cannot see the JSP, as this is directly converted by the server, being presented to the user only the HTML.

Prerequisites

  • Ubuntu 20.04 or Ubuntu 20.10
  • 2048 MB or above Ram
  • 10 GB Disk Space
  • 2 vCPU or above CPU
  • Internet connection
  • root privileges

Directory structure

I found a way to make a minimal JSP web application from scratch, without any IDE.

Step 1 - 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.

Step 2 - 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 < 2; ++i) {
      out.println("<p> Hello, world LetsCloud! (: </p>");
  }
%>

This is just an example; it will produce:

<h1> It works! </h1>
<p> Hello, world LetsCloud! (: </p><p> Hello, world LetsCloud! (: </p>

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

Step 3 - Dockerfile

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

FROM tomcat:latest

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

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

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

Apache Tomcat Image

FROM tomcat:latest

We use official tomcat:latest image for our base image. You can choose any other images you want, like tomcat is the latest release of Apache Tomcat image on Docker Hub.

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 ["bash.sh", "run"]

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

Step 4 - Build Docker Image and Run

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

$ docker build -t letscloud .

Log in to Docker Hub from the Docker CLI

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: sinesio.bittencourt
Password:
Login Succeeded

Once the Login succeeded.

Based on your Internet speed you can see the Upload gets completed in a couple of minutes.

$ docker push letscloud

The push refers to repository
bfc9fd0939b7: Pushed
f6b1f20ff3ed: Pushed
6d21360e64a0: Pushed
4a62c00a3c78: Pushed
0bc97febfdbb: Pushed
0f861fa8dd99: Pushed
c1117571ffb4: Pushed
latest: digest: sha256:8ede588ad0bb944e62765fdb40b21f7ebdc6587afa00efc7570ede4c7f0786fa size: 1996

Once the image is uploaded (or) Pushed, The entire world can reuse your image as we have mentioned earlier using docker pull

Step 6 - And then run:

$ docker run --rm -itd --name tomcatletscloud -p 8888:8080 letscloud

-it: to enable Interactive Session/SSH to get into the container at a later point in time

-d: Run the container in the background (always recommended)

--name: name your container

-p 8888:8080: Forwarding the Container port 8080 to Host 8888

To quickly verify, if your container use docker ps command. Then at the end of this post, we will share more commands to manage your container

Visit http://localhost:8080 to see if your website is running! \o/

Step 7 - Checking webapps Directory in Tomcat Container

Inspect to tomcat container, and navigate to the default web folder of tomcat.

$ docker exec -it tomcatletscloud sh
$ cd webapps
$ ls
WEB-INF/	classes/	lib/	web.xml	index.jsp
$ pwd
/usr/local/tomcat/webapps

BONUS! - Docker Commands Cheat Sheet

# How to start and stop the container 

docker stop <containername/id>  # to stop the container which is running in background mode
docker container start <containername/id> # to start the already stopped container

# monitoring the Container 

docker container list # list the containers created
docker container ls -a  #to list the containers including not running containers
docker ps    #to list the running container
docker info #docker engine status including container running/paused/stopped containers list
docker container stats <containername/id> #prints the CPU and MEM usage data of the container name
docker container stats #prints the CPU and MEM usage of all running containers
docker container top <containername/id> #executes the top command in the container specified, fails if the specified container is not running

# How to remove/delete the container

docker container rm <containername/id> # Remove the Container 

# How to remove/delete the image

docker container rmi <imagename/imageid> # Remove the image
# How to view the logs of the running container

docker container logs <containername/id> # to view the logs of the container 

Conclusion

If you hit a problem or have feedback, leave a comment below.