将 Java webapp 部署到在 Docker 容器中运行的 Tomcat 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29201262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:33:54  来源:igfitidea点击:

Deploying Java webapp to Tomcat 8 running in Docker container

javatomcatdockerspring-bootwar

提问by Chris

I am pretty new to Tomcat and Docker - so I am probably missing a Tomcat fundamental somewhere in this question.

我对 Tomcat 和 Docker 还很陌生——所以我可能在这个问题的某个地方遗漏了一个 Tomcat 基础知识。

What I am trying to do is build a Docker container that runs a SpringBoot Restful web service that just returns some static data. This is all running on OSX so I am using Boot2Docker as well.

我想要做的是构建一个运行 SpringBoot Restful Web 服务的 Docker 容器,该服务仅返回一些静态数据。这一切都在 OSX 上运行,所以我也在使用 Boot2Docker。

I've written my own Dockerfile to build the container that my app runs in:

我已经编写了自己的 Dockerfile 来构建我的应用程序在其中运行的容器:

FROM tomcat:8.0.20-jre8

RUN mkdir /usr/local/tomcat/webapps/myapp

COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp/

This Dockerfile works fine and I am able to start the container from the created image.

这个 Dockerfile 工作正常,我可以从创建的映像启动容器。

docker build -t myapp .

docker run -it --rm -p 8888:8080 myapp

This container starts correctly and outputs no errors and displays the message saying my app was deployed.

此容器正确启动,不输出任何错误,并显示消息说我的应用程序已部署。

22-Mar-2015 23:07:21.217 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 
Deploying web application directory /usr/local/tomcat/webapps/myapp

The container also correctly has the myapp.war copied to the path described in the Dockerfile. Moreover I am able to navigate to Tomcat default page to confirm that Tomcat is running, I can also hit all the examples, etc.

容器也正确地将 myapp.war 复制到 Dockerfile 中描述的路径。此外,我可以导航到 Tomcat 默认页面以确认 Tomcat 正在运行,我还可以点击所有示例等。

To the problem, when I navigate to http://192.168.59.103:8888/myapp/getDataI get a 404. I can't quite figure out why. Am I missing something regarding a .war deploy to Tomcat?

对于这个问题,当我导航到http://192.168.59.103:8888/myapp/getData 时,我得到一个 404。我不太明白为什么。我是否遗漏了一些关于 .war 部署到 Tomcat 的内容?

采纳答案by crazyman

You are trying to copy the warfile to a directory below webapps. The war file should be copied into the webappsdirectory.

您正试图将war文件复制到下面的目录中webapps。应将war 文件复制到该webapps目录中。

Remove the mkdir command, and copy the warfile like this:

删除 mkdir 命令,并war像这样复制文件:

COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war

Tomcat will extract the war if autodeployis turned on.

如果autodeploy打开,Tomcat 将提取战争。

回答by Saril Sudhakaran

Tomcat will only extract the war which is copied to webappsdirectory. Change Dockerfileas below:

Tomcat 只会提取复制到webapps目录中的战争。更改Dockerfile如下:

FROM tomcat:8.0.20-jre8
COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war

You might need to access the url as below unless you have specified the webroot

除非您指定了 webroot,否则您可能需要访问如下 url

http://192.168.59.103:8888/myapp/getData

http://192.168.59.103:8888/myapp/getData

回答by Krishna Chaitanya

There's a oneliner for this one.

这个有一个oneliner。

You can simply run,

你可以简单地运行,

docker run -v /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat

This will copy the war file to webapps directory and get your app running in no time.

这会将 war 文件复制到 webapps 目录并立即运行您的应用程序。