Dockerize Apache Tomcat Servlets
Tags: tomcat, apache, java, docker
Published: 2021-06-01 11:47:13

## Step 1 - Dependencies
Make sure you have `Java EE`, a compatible version of `JDK` and `Docker` installed.
If you don't have `Java EE` installed, or if you have `OpenJDK` , you can use the `Java EE` jar file downloaded from [this link](http://www.java2s.com/Code/Jar/j/Downloadjavaeejar.htm)

## Step 2 - Compile source code

Let's use the Java source code from [this link](https://www.informit.com/articles/article.aspx?p=26920&seqNum=4) and compile it using
```bash
javac -cp .:<path to javaee jar> -target 1.7 -source 1.7 TestingServlet.java
```
> If you're using windows, you have to use `;` instead of `:` in classpath
> The `-target` is the version number of the runtime environment of tomcat. Similarly for `-source`

## Step 3 - Environment Setup
Let's use the `Dockerfile` from [here](https://github.com/softwareyoga/docker-tomcat-tutorial) and let's add some stuff.

```Dockerfile
FROM tomcat:8.0-alpine
LABEL maintainer="deepak@softwareyoga.com"

COPY ./web.xml /usr/local/tomcat/webapps/ROOT/WEB-INF
COPY ./TestingServlet.class /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/TestingServlet.class
COPY ./tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xml

EXPOSE 8080
CMD ["catalina.sh", "run"]
```
> We define `web.xml` and `tomcat-users.xml` later. The file `TestingServlet.class` is important and we'll remember the name for later.

## Step 4 - Configuration

Let's define `web.xml` as follows:
```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <servlet>
   <servlet-name>Testing</servlet-name>
   <servlet-class>TestingServlet </servlet-class>
  </servlet>

 <servlet-mapping>
 <servlet-name>Testing</servlet-name>
 <url-pattern>/servlet/TestingServlet</url-pattern>
 </servlet-mapping>
</web-app>

```
> `servlet-class` is the name of the `.class` file and `url-pattern` is the `HTTP` url you want to access it by.

Let's define `tomcat-users.xml` as follows: 
```xml
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->

  <role rolename="manager"/>
  <role rolename="manager-gui"/>
  <user username="admin" password="admin" roles="manager-gui"/>
</tomcat-users>
```
> Apache Tomcat defines a list of roles [here](http://tomcat.apache.org/migration-7.html)

## Step 5 - Deployment
Let's build the `Docker` image 
```bash
docker build -t tomcat .
```

And run it with 
```bash
docker run -d -p 8080:8080 --name tomcat tomcat
```
> Apache Tomcat runs on port `8080` in the container




You can now visit `http://localhost:8080/servlet/TestingServlet` and see the following message
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adrey0a3xxy8iyt61jgh.png)
 
## Step 6 - Debugging

If you see an error like this 
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcqmir1sdr65blvdifdy.png)
It means that you have to select a differente `target` and `source` version, either higher or lower depending on the version of the target runtime. You can check the Java Runtime version through the [Tomcat UI](http://localhost:8080/manager/html) at the bottom of the page
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/291eg56nc7zhvj2burvi.png)
 
Alternatively, you can run `bash version.sh` in `/usr/local/tomcat/bin` when inside the running Docker container

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ogab8ldnxtbr1r6dv4w.png)
 

### References
1. [Tomcat Docker](https://github.com/softwareyoga/docker-tomcat-tutorial)
2. [Java cross compile](https://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#crosscomp-example)
3. [Servlet setup](https://www.slideshare.net/tusharkute/java-servlet-programming-under-ubuntu-linux-by-tushar-b-kute)
4. [Servlet setup 2](https://www.informit.com/articles/article.aspx?p=26920&seqNum=4)
5. [Apache Tomcat Roles](http://tomcat.apache.org/migration-7.html)