DEV Community

David Velho
David Velho

Posted on • Updated on

Dockerize Apache Tomcat Servlets

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

Step 2 - Compile source code

Let's use the Java source code from this link and compile it using

javac -cp .:<path to javaee jar> -target 1.7 -source 1.7 TestingServlet.java
Enter fullscreen mode Exit fullscreen mode

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 and let's add some stuff.

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"]
Enter fullscreen mode Exit fullscreen mode

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 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>

Enter fullscreen mode Exit fullscreen mode

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 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>
Enter fullscreen mode Exit fullscreen mode

Apache Tomcat defines a list of roles here

Step 5 - Deployment

Let's build the Docker image

docker build -t tomcat .
Enter fullscreen mode Exit fullscreen mode

And run it with

docker run -d -p 8080:8080 --name tomcat tomcat
Enter fullscreen mode Exit fullscreen mode

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

Step 6 - Debugging

If you see an error like this
alt text
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 at the bottom of the page
alt text

Alternatively, you can run bash version.sh in /usr/local/tomcat/bin when inside the running Docker container

alt text

References

  1. Tomcat Docker
  2. Java cross compile
  3. Servlet setup
  4. Servlet setup 2
  5. Apache Tomcat Roles

Top comments (0)