Skip to content

Commit 34487d7

Browse files
committed
Containerise application
1 parent 92d7850 commit 34487d7

File tree

7 files changed

+231
-0
lines changed

7 files changed

+231
-0
lines changed

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM maven:3.8.6-openjdk-8-slim AS build
2+
WORKDIR /app
3+
COPY . .
4+
RUN mvn clean install
5+
6+
FROM tomcat:9.0-jre8-temurin
7+
# Tomcat Docker Hub page (https://hub.docker.com/_/tomcat) has this warning:
8+
# As of docker-library/tomcat#181 (https://github.com/docker-library/tomcat/pull/181)⁠, the upstream-provided (example)
9+
# webapps are not enabled by default, per upstream's security recommendations.
10+
# (https://tomcat.apache.org/tomcat-9.0-doc/security-howto.html#Default_web_applications)⁠,
11+
# These apps are still available under the webapps.dist folder within the image to make them easier to re-enable.
12+
# So we move some folders around to re-enable the default behaviour
13+
RUN mv /usr/local/tomcat/webapps /usr/local/tomcat/webapps.empty
14+
RUN mv /usr/local/tomcat/webapps.dist /usr/local/tomcat/webapps
15+
COPY --from=build /app/target/DemoServlet.war /usr/local/tomcat/webapps/
16+
17+
# Enable manager mode
18+
COPY --from=build /app/tomcat/manager/context.xml /usr/local/tomcat/webapps/manager/META-INF/
19+
COPY --from=build /app/tomcat/tomcat-users.xml /usr/local/tomcat/conf/
20+
21+
# Enable examples access from remote host
22+
COPY --from=build /app/tomcat/examples/context.xml /usr/local/tomcat/webapps/examples/META-INF/
23+
24+
# Logging
25+
COPY --from=build /app/tomcat/logging.properties /usr/local/tomcat/conf/
26+
27+
EXPOSE 8080
28+
CMD ["catalina.sh", "run"]

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Requires Java 8 update 442 `1.8.0_442`
2626
3. Add your database connection settings to `DBConnection.java`
2727
4. Run the application on Apache Tomcat server
2828

29+
If you have Docker, run `docker compose up` and visit `http://localhost:4000/demo/` in the browser.
30+
2931
### Screenshots
3032

3133
If everything is up and running properly, you should be able to use the following functionality:

compose.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
services:
2+
app:
3+
build:
4+
dockerfile: Dockerfile
5+
container_name: demo-java-ee-servlet
6+
image: demo-java-ee-servlet:1.0
7+
ports:
8+
- 4000:8080
9+
environment:
10+
MYSQL_DB: db
11+
MYSQL_HOST: demo-java-ee-servlet-db
12+
MYSQL_PASSWORD: password
13+
MYSQL_USER: user
14+
database:
15+
image: mysql:latest
16+
container_name: demo-java-ee-servlet-db
17+
environment:
18+
MYSQL_DATABASE: db
19+
MYSQL_PASSWORD: password
20+
MYSQL_ROOT_PASSWORD: password
21+
MYSQL_USER: user
22+
volumes:
23+
- ./database:/docker-entrypoint-initdb.d:ro

tomcat/examples/context.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<Context>
19+
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
20+
sameSiteCookies="strict" />
21+
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
22+
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
23+
</Context>

tomcat/logging.properties

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
17+
18+
.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
19+
20+
############################################################
21+
# Handler specific properties.
22+
# Describes specific configuration info for Handlers.
23+
############################################################
24+
25+
1catalina.org.apache.juli.AsyncFileHandler.level = ALL
26+
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
27+
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
28+
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90
29+
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
30+
31+
2localhost.org.apache.juli.AsyncFileHandler.level = ALL
32+
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
33+
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
34+
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
35+
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8
36+
37+
3manager.org.apache.juli.AsyncFileHandler.level = ALL
38+
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
39+
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
40+
3manager.org.apache.juli.AsyncFileHandler.maxDays = 90
41+
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
42+
43+
4host-manager.org.apache.juli.AsyncFileHandler.level = ALL
44+
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
45+
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
46+
4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90
47+
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
48+
49+
java.util.logging.ConsoleHandler.level = ALL
50+
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
51+
java.util.logging.ConsoleHandler.encoding = UTF-8
52+
53+
54+
############################################################
55+
# Facility specific properties.
56+
# Provides extra control for each logger.
57+
############################################################
58+
59+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
60+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler
61+
62+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
63+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler
64+
65+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
66+
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler
67+
68+
# For example, set the org.apache.catalina.util.LifecycleBase logger to log
69+
# each component that extends LifecycleBase changing state:
70+
# org.apache.catalina.util.LifecycleBase.level = FINE
71+
72+
# To see debug messages for HTTP/2 handling, uncomment the following line:
73+
# org.apache.coyote.http2.level = FINE
74+
75+
# To see debug messages for WebSocket handling, uncomment the following line:
76+
# org.apache.tomcat.websocket.level = FINE

tomcat/manager/context.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<Context antiResourceLocking="false" privileged="true" >
19+
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
20+
sameSiteCookies="strict" />
21+
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
22+
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
23+
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
24+
</Context>

tomcat/tomcat-users.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<tomcat-users xmlns="http://tomcat.apache.org/xml"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
21+
version="1.0">
22+
<!--
23+
By default, no user is included in the "manager-gui" role required
24+
to operate the "/manager/html" web application. If you wish to use this app,
25+
you must define such a user - the username and password are arbitrary.
26+
27+
Built-in Tomcat manager roles:
28+
- manager-gui - allows access to the HTML GUI and the status pages
29+
- manager-script - allows access to the HTTP API and the status pages
30+
- manager-jmx - allows access to the JMX proxy and the status pages
31+
- manager-status - allows access to the status pages only
32+
33+
The users below are wrapped in a comment and are therefore ignored. If you
34+
wish to configure one or more of these users for use with the manager web
35+
application, do not forget to remove the <!.. ..> that surrounds them. You
36+
will also need to set the passwords to something appropriate.
37+
-->
38+
<user username="admin" password="password" roles="manager-gui"/>
39+
<!-- <user username="robot" password="<must-be-changed>" roles="manager-script"/> -->
40+
41+
<!--
42+
The sample user and role entries below are intended for use with the
43+
examples web application. They are wrapped in a comment and thus are ignored
44+
when reading this file. If you wish to configure these users for use with the
45+
examples web application, do not forget to remove the <!.. ..> that surrounds
46+
them. You will also need to set the passwords to something appropriate.
47+
-->
48+
<!--
49+
<role rolename="tomcat"/>
50+
<role rolename="role1"/>
51+
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
52+
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
53+
<user username="role1" password="<must-be-changed>" roles="role1"/>
54+
-->
55+
</tomcat-users>

0 commit comments

Comments
 (0)