Search code examples
javadockercontainersdetach

How can i detach from integrated tomcat from a springboot application


I have a project in springboot, and when i run mvn spring-boot:run it deploy the app with a embeded tomcat, i need to run a container with tomcat, and compile the app to a WAR, and add it to the tomcat container.


Solution

  • Thanks man by answering me, finally i figured out how to do it.

    First to access admin dashboard of Tomcat, we need to create a set of files like this:

    -MySpringBootApp
    |-->src
    |----|->myspringbootapppackage
    |------------------|->MySpringBootApp.java
    |-->Dockerfile
    |-->tomcat-users.xml
    |-->pom.xml
    

    at tomcat-users.xml i wrote like this

    <tomcat-users>
        <role rolename="admin-gui"/>
        <user username="tomcat" password="asd123" roles="manager-gui"/>
    </tomcat-users>
    

    At my Dockerfile i wrote like this:

    FROM tomcat:7-jre8-alpine
    LABEL maintainer="myemail@myemail.mytld"
    EXPOSE 8080
    ARG WAR_FILE=target/myappbuilded-0.0.1-SNAPSHOT.war
    ARG TOMCAT_USERS_FILE=tomcat-users.xml
    ADD ${WAR_FILE} /usr/local/tomcat/webapps/myappbuilded.war
    ADD ${TOMCAT_USERS_FILE} /usr/local/tomcat/conf
    CMD ["catalina.sh", "run"]
    

    At my pom.xml i added this plugin at dependecies and also add at the top of the file the node with name packaging

    <packaging>war</packaging>
    </dependencies>
        ...other dependencies ...
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
    

    Finally at MySpringBootApp.java i just add an extends from SpringBootServletInitializer and also add an override method configure the file looks like this:

    package myspringbootpackage;
    
    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 MySpringBootApp extends SpringBootServletInitializer {
    
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(MySpringBootApp.class);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApp.class, args);
        }
    }
    

    Thats it! finally i just compile my app by running mvn clean package -Dmaven.test.skip=true disabling the tests, and once the app is compiled to target directory, and i compiled the docker image with sudo docker build -t myspringbootappcontainer . inside the app directory ofcourse. Finally run my container by running sudo docker run -p 5000:8080 myspringbootappcontainer. Thats it :) Hope it helps the most to other developers :)