Issue
I use an image of tomcat-9 for adoptopenjdk. The docker-compose file config is:
tomcat-server:
image: tomcat:9.0.45-jdk11-adoptopenjdk-hotspot
ports:
- "8081:8080"
volumes:
- /webapp/target/app.war:/usr/local/tomcat/webapps/app.war
environment:
- EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://naming-server:8761/eureka
- SPRING.REDIS.HOST=redis-cache-server
When I start the tomcat using docker-compse file, the logs shows that it wont start with the port mentioned 8081. It is still started on 8080. See the logs for details.
tomcat-server_1 | NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.45
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 30 2021 10:29:04 UTC
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.45.0
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.4.72-microsoft-standard-WSL2
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Java Home: /opt/java/openjdk
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 11.0.10+9
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: AdoptOpenJDK
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
tomcat-server_1 | org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
tomcat-server_1 | org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.27] using APR version [1.6.5].
tomcat-server_1 | org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
tomcat-server_1 | org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
tomcat-server_1 | org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1f 31 Mar 2020]
tomcat-server_1 | org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
tomcat-server_1 | org.apache.catalina.startup.Catalina.load Server initialization in [745] milliseconds
tomcat-server_1 | org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
tomcat-server_1 | org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.45]
tomcat-server_1 | org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/app.war]
tomcat-server_1 | org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/app.war] has finished in [361] ms
tomcat-server_1 | org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
tomcat-server_1 | org.apache.catalina.startup.Catalina.start Server startup in [463] milliseconds
I tried few things but not having any success. Am I missing something?
Thanks,
Atul
Solution
I am able to solve the issue. Solution is to create a separate docker file to create a tomcat image and used it in docker composer file.
The problem is, even the war is copied to webapps folder of tomcat it wont deploy. See the logs for reference:
tomcat-server_1 | org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/app.war]
tomcat-server_1 | org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/app.war] has finished in [361] ms
tomcat-server_1 | org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
As image tomcat:9.0.45-jdk11-adoptopenjdk-hotspot contains the command ['catalina.sh', 'run'], as soon as the docker-compose command executes, the tomcat started and at the end of the starting process it copied war file to the tomcat location. So it wont deploy the war file.
This is the reason what I have found. (Please clear me if I am wrong ot I missed anything).
So final solution is like this(which worked for me):
Create a docker file. (e.g.- Dockerfile_web)
#Take the Tomcat-9 image which supports AdoptOpenJDK
FROM tomcat:9.0.45-jdk11-adoptopenjdk-hotspot
#Copy the WAR file to tomcat
ADD ./webapp/target/attest.war /usr/local/tomcat/webapps/attest.war
#Allow execution (Not required, just added)
CMD chmod +x /usr/local/tomcat/bin/catalina.sh
Next is to add this file reference in docker-compose file.
web-app:
build:
context: .
dockerfile: Dockerfile_Web
ports:
- "8081:8080"
networks:
- app-network
command: ['catalina.sh', 'run']
environment:
- EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://naming-server:8761/eureka
- SPRING.REDIS.HOST=redis-cache-server
Once I run this with docker-compose up it started and working as expexcted.
Answered By - Atul