我已经关注了 Spring 的 Building a RESTful Web Service教程并创建了一个虚拟 webapp(带有“使用 Maven 构建”说明)。我构建并打包了 WAR。然后我用这个命令运行它:
java -jar ./target/Dummy-1.0-SNAPSHOT.war
我可以在 http://localhost:8080/greeting/ 看到虚拟 JSON 端点.
现在我想用 Docker 将应用程序容器化,这样我就可以进一步测试它,而无需将 Tomcat 安装到系统空间。这是我创建的 Dockerfile
:
FROM tomcat:7-jre8-alpine
# copy the WAR bundle to tomcat
COPY /target/Dummy-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/app.war
# command to run
CMD ["catalina.sh", "run"]
我构建并运行 docker 绑定(bind)到 http://localhost:8080 .我可以在“http://localhost:8080”上看到 Tomcat 欢迎页面。但我都看不到我的应用:
我应该如何追踪问题?可能是什么问题?
更新 1:Tomcat 管理界面截图
最佳答案
the example 中的 Application.java
文件看起来像这样:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是一个有效的 SpringBoot 应用程序,但不是 Tomcat 的可部署应用程序。要使其可部署,您可以:
- 重新定义
Application
以从 Spring 框架 web 支持扩展SpringBootServletInitializer
;那么 覆盖
configure
方法:package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
无需更改 pom.xml
文件(或任何其他配置)。
重建 dockerfile 并使用正确的端口绑定(bind)运行它后,问候示例端点将通过以下方式提供:http://localhost:8080/app/greeting/
引用文献
关于java - 将 Spring WAR 部署到基于 Tomcat 的 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44919972/