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
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"]
We define
web.xml
andtomcat-users.xml
later. The fileTestingServlet.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>
servlet-class
is the name of the.class
file andurl-pattern
is theHTTP
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>
Apache Tomcat defines a list of roles here
Step 5 - Deployment
Let's build the Docker
image
docker build -t tomcat .
And run it with
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
Step 6 - Debugging
If you see an error like this
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
Alternatively, you can run bash version.sh
in /usr/local/tomcat/bin
when inside the running Docker container
Top comments (0)