如何使用 Docker 卷在 Tomcat 中部署 war / jar
是否可以在Tomcat中部署一些java war或jar文件?我寻找了很多教程,我发现的唯一解决方案是将项目war文件复制到./usr/local/tomcat/webapps/
我实际上使用了该解决方案,但我想改善我的dockerization。我的主要目标是当我使用docker-compose运行我的2个映像(tomcat中的应用程序和db映像)时,我想使用tomcat中目标文件夹的本地war文件,并且,当我在代码更改后再次构建war时,该更改将反映在不停止容器,删除和重建的情况下。你能帮忙吗?我的尝试失败了。我希望它仅用于开发目的。
这是我的docker-compose.yml
version: '3'
services:
tomcat-service:
build:
context: ../
dockerfile: docker/app/Dockerfile
volumes:
- D:\myproj\target\app.war:/usr/local/tomcat/webapps/ROOT.war
ports:
- "8080:8080"
depends_on:
- "db-service"
db-service:
build: ./database
ports:
- "5433:5432"
和那个雄猫的Dockerfile
FROM tomcat:8.0-jre8
RUN rm -rvf /usr/local/tomcat/webapps/ROOT
COPY ./docker/app/context.xml /usr/local/tomcat/conf/
# with following copy command it works, but when I rebuild war file, I need stop docker-compose and build and run it again .. I want use volume instead of copy war
#COPY ./pnp-web/target/pnp.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]
使用上面的配置,应用程序启动,但当我运行应用程序时不再加载mvn clean package
编辑
我检查了tomcat容器的日志,我发现了这个错误:
tomcat-cont | 10-Jul-2018 08:20:36.754 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/ROOT.war
tomcat-cont | 10-Jul-2018 08:20:36.858 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
tomcat-cont | org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
tomcat-cont | at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
tomcat-cont | at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
tomcat-cont | at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
tomcat-cont | at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
tomcat-cont | at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:973)
tomcat-cont | at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1850)
tomcat-cont | at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
tomcat-cont | at java.util.concurrent.FutureTask.run(FutureTask.java:266)
tomcat-cont | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
tomcat-cont | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
tomcat-cont | at java.lang.Thread.run(Thread.java:748)
tomcat-cont | Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@51f50cb1]
tomcat-cont | at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
tomcat-cont | at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:5016)
tomcat-cont | at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5149)
tomcat-cont | at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
tomcat-cont | ... 10 more
tomcat-cont | Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@20e48a4a]
tomcat-cont | at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
tomcat-cont | at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:135)
tomcat-cont | at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:722)
tomcat-cont | at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
tomcat-cont | ... 13 more
tomcat-cont | Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: error in opening zip file
tomcat-cont | at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:142)
tomcat-cont | at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
tomcat-cont | ... 16 more
tomcat-cont | Caused by: java.util.zip.ZipException: error in opening zip file
tomcat-cont | at java.util.zip.ZipFile.open(Native Method)
tomcat-cont | at java.util.zip.ZipFile.<init>(ZipFile.java:225)
tomcat-cont | at java.util.zip.ZipFile.<init>(ZipFile.java:155)
tomcat-cont | at java.util.jar.JarFile.<init>(JarFile.java:166)
tomcat-cont | at java.util.jar.JarFile.<init>(JarFile.java:130)
tomcat-cont | at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:170)
tomcat-cont | at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:155)
tomcat-cont | at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:139)
tomcat-cont | ... 17 more
tomcat-cont |
tomcat-cont | 10-Jul-2018 08:20:36.859 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive /usr/local/tomcat/webapps/ROOT.war
tomcat-cont | java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].Stand
ardContext[]]
tomcat-cont | at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
tomcat-cont | at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
tomcat-cont | at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
tomcat-cont | at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:973)
tomcat-cont | at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1850)
tomcat-cont | at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
tomcat-cont | at java.util.concurrent.FutureTask.run(FutureTask.java:266)
tomcat-cont | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
tomcat-cont | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
tomcat-cont | at java.lang.Thread.run(Thread.java:748)
tomcat-cont |
tomcat-cont | 10-Jul-2018 08:20:36.860 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/ROOT.war has finish
ed in 105 ms
当我想在构建新战争时尝试重新启动容器时,会发生此错误。