0
votes

Here is my Dockerfile:

FROM tomcat:8.0-alpine

RUN rm -rf /Users/firstname.lastname/Desktop/Backup/apache-tomcat-9.0.30/webapps/*

ADD ./target/restfullapi-0.0.1-SNAPSHOT.war /Users/firstname.lastname/Desktop/Backup/apache-tomcat-9.0.30/webapps/restfullapi.war

EXPOSE 8089

CMD ["catalina.sh", "run"]


Once I build the .war file I do the following steps:

docker build -t restfullapi.war . docker run -d -p 8085:8089 restfullapi.war Now when I open localhost:8085 I am able to see only tomcat home page which is correct, but the problem is If I try to access -->> localhost:8085/restfullapi/movies I get 404 error.

and here is my pom.xml


http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.dev.movies restfullapi war 0.0.1-SNAPSHOT RestFulAPI Maven Webapp http://maven.apache.org junit junit 3.8.1 test

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>

</dependencies>
<build>
    <finalName>restfullapi</finalName>
</build>

1
why do you want to run a Spring Boot app as Tomcat WAR? - Beppe C
Hi @BeppeC,requirement was to run my application in Docker - Shivu B Sasanur
You can Dockerise the SpringBoot application (without Tomcat) creating the jar will all dependencies and run on top of the embedded Tomcat - Beppe C
@Beppe C I am curious to know about why do we use >jar and .war files ? is there a huge difference of running them in docker? I mean what happens when I use .jar & what happens when I use .war file in docker? - Shivu B Sasanur
The final result is the same but to run a WAR you need a container (Tomcat) so your architecture would be more complicated and relying on Tomcat. SpringBoot can run using the embedded container (still Tomcat) which you dont need to configure or maintain, simplify the deployment - Beppe C

1 Answers

0
votes

You can run a SpringBoot application in Docker using the default embedded Tomcat. This avoids the need to create a WAR file and provide Tomcat yourself.

FROM adoptopenjdk/openjdk11:latest

RUN mkdir -p /software/app

ADD target/restfullapi.jar /software/app/restfullapi.jar

ENV port=8888

CMD java -jar /software/app/restfullapi.jar -Dspring.profiles.active=${SPRING_ACTIVE_PROFILE}