gpt4 book ai didi

java - 如何在 Tomcat 容器上运行 .jar?

转载 作者:行者123 更新时间:2023-11-28 23:10:27 24 4
gpt4 key购买 nike

我是 docker 的新手,我正在尝试在 tomcat:8.0-alpine 容器中运行 maven-wrapper.jar 文件。我按照以下网站 https://medium.com/@pra4mesh/deploy-war-in-docker-tomcat-container-b52a3baea448 中的一些步骤操作区别在于它在我使用 .jar 文件时使用 .war。我不知道这些信息是否对您有用,但是使用 https://start.spring.io/ 自动生成项目。

这是我用来构建镜像的 Dockerfile(老实说,我不知道什么叫 Dockerfile 或其他东西)。

FROM tomcat:8.0-alpine

LABEL maintainer="deepak@softwareyoga.com"

ADD maven-wrapper.jar /usr/local/tomcat/webapps/

EXPOSE 8080

CMD ["catalina.sh", "run"]

请注意,当我使用以下命令构建图像时:

\Desktop\try>docker build .
Sending build context to Docker daemon 53.76kB
Step 1/5 : FROM tomcat:8.0-alpine
8.0-alpine: Pulling from library/tomcat
4fe2ade4980c: Already exists
6fc58a8d4ae4: Already exists
7d9bd64c803b: Already exists
a22aedc5ac11: Already exists
5bde63ae3587: Already exists
69cb0c9b940a: Already exists
Digest: sha256:d02a16c0147fcae13d812fa670a4b3c9944f5328b10a5a463ad697d2aa5bb063
Status: Downloaded newer image for tomcat:8.0-alpine
---> 624fb61775c3
Step 2/5 : LABEL maintainer="deepak@softwareyoga.com"
---> Running in 1d725bda89d9
Removing intermediate container 1d725bda89d9
---> 11b904dae739
Step 3/5 : ADD maven-wrapper.jar /usr/local/tomcat/webapps/
---> ed694b383b75
Step 4/5 : EXPOSE 8080
---> Running in 2dc19f215bc7
Removing intermediate container 2dc19f215bc7
---> 59981a644862
Step 5/5 : CMD ["catalina.sh", "run"]
---> Running in 08f6b5d8d632
Removing intermediate container 08f6b5d8d632
---> c8576389fd76
Successfully built c8576389fd76
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double-check and reset permissions for sensitive files and directories.

“成功构建”它可以工作并生成图像

\Desktop\try>docker 图像存储库标记图像 ID 创建大小 c8576389fd76 6 分钟前 147MBtomcat 8.0-alpine 624fb61775c3 14 个月前 147MB

my jar file name maven-wrapper.jar 
and exist in same folder with Dockerfile
>Desktop\try>dir
11/08/2019 10:33 PM <DIR> .
11/08/2019 10:33 PM <DIR> ..
11/08/2019 11:24 PM 301 Dockerfile
11/08/2019 05:27 PM 50,710 maven-wrapper.jar
2 File(s) 51,011 bytes
2 Dir(s) 269,240,381,440 bytes free

\Desktop\try>docker container run -it --publish 8081:8080 c8576389fd76
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/lib/jvm/java-1.7-openjdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
08-Nov-2019 21:52:57.955 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
08-Nov-2019 21:52:57.958 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC
08-Nov-2019 21:52:57.959 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0
08-Nov-2019 21:52:57.962 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
08-Nov-2019 21:52:57.964 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.9.184-linuxkit
08-Nov-2019 21:52:57.965 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
08-Nov-2019 21:52:57.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.7-openjdk/jre
08-Nov-2019 21:52:57.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01
08-Nov-2019 21:52:57.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
08-Nov-2019 21:52:57.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
08-Nov-2019 21:52:57.968 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
08-Nov-2019 21:52:57.969 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
08-Nov-2019 21:52:57.970 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
08-Nov-2019 21:52:57.971 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
08-Nov-2019 21:52:57.976 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
08-Nov-2019 21:52:57.979 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
08-Nov-2019 21:52:57.981 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
08-Nov-2019 21:52:57.983 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
08-Nov-2019 21:52:57.983 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
08-Nov-2019 21:52:57.984 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.6.3.
08-Nov-2019 21:52:57.985 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
08-Nov-2019 21:52:57.991 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2o 27 Mar 2018)
08-Nov-2019 21:52:58.082 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
08-Nov-2019 21:52:58.092 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
08-Nov-2019 21:52:58.095 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 766 ms
08-Nov-2019 21:52:58.127 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
08-Nov-2019 21:52:58.127 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
08-Nov-2019 21:52:58.151 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
08-Nov-2019 21:52:58.907 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 756 ms
08-Nov-2019 21:52:58.908 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
08-Nov-2019 21:52:59.001 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 93 ms
08-Nov-2019 21:52:59.002 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
08-Nov-2019 21:52:59.427 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 425 ms
08-Nov-2019 21:52:59.428 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
08-Nov-2019 21:52:59.455 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 27 ms
08-Nov-2019 21:52:59.456 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
08-Nov-2019 21:52:59.482 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 26 ms
08-Nov-2019 21:52:59.489 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
08-Nov-2019 21:52:59.504 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
08-Nov-2019 21:52:59.509 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1413 ms

转到 http://localhost:8081向我展示了一个 tomcat 服务器默认页面我试试 http://localhost:8081/maven-wrapper“HTTP 状态 404 – 未找到有什么帮助吗?!!

最佳答案

在您的项目中,您正在使用 spring-boot-starter-web 依赖项,它提供了一个嵌入式 Tomcat servlet 容器。除非您更改项目设置以生成 WAR,否则无需使用提供 Tomcat 的 Docker 镜像。

首先阅读如何 Spring Boot Maven Plugin工作原理以及它如何打包可执行 JAR。一旦您了解了该过程,就可以使用仅提供 Java 运行时的 Docker 镜像,例如openjdk .

关于java - 如何在 Tomcat 容器上运行 .jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58774290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com