Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Spring WAR to Tomcat-based docker

I have followed through Spring's Building a RESTful Web Service tutorial and created a dummy webapp (with "Build with Maven" instructions). I build and package the WAR. Then I run it with this command:

java -jar ./target/Dummy-1.0-SNAPSHOT.war

I can see the dummy JSON endpoint at http://localhost:8080/greeting/.

Now I want to containerize the app with Docker so I can further test it without the needs to install Tomcat to system space. This is the Dockerfile I created:

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"]

I build and run the docker binding to http://localhost:8080. I can see the Tomcat welcome page on "http://localhost:8080". But I couldn't see my app on neither:

  • http://localhost:8080/app/
  • http://localhost:8080/app/greeting/
  • http://localhost:8080/greeting/

How should I track down the issue? What could be the problem?

Update 1: The Tomcat admin interface screenshot

Tomcat Admin

like image 678
Koala Yeung Avatar asked Jul 05 '17 07:07

Koala Yeung


People also ask

Can I deploy war to Docker?

war, which we need to deploy to the Tomcat server. To achieve our goal, we need to first create a Dockerfile. This Dockerfile will include all the dependencies necessary to run our application. Further, we'll create a Docker image using this Dockerfile followed by the step to launch the Docker container.

Can I deploy Spring boot jar to Tomcat?

You can not deploy Jar to tomcat and expect it to load your web application.


1 Answers

The Application.java file in the example looks like this:

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);
    }
}

This is a valid SpringBoot application, but NOT a deployable application to Tomcat. To make it deployable, you can can:

  1. redefineApplication to extend SpringBootServletInitializer from Spring framework web support; then
  2. override the configure method:

    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);
        }
    
    }
    

No need to change the pom.xml file (or any other configurations).

After rebuilding the dockerfile and run it with proper port binding, the greeting example endpoint will be available through: http://localhost:8080/app/greeting/

References

  1. Spring Boot War deployed to Tomcat
  2. Spring Boot Reference Guide: Create a deployable war file
like image 117
Koala Yeung Avatar answered Nov 01 '22 19:11

Koala Yeung