Denis Stephanov
Denis Stephanov

Reputation: 5241

Create and run docker image for Spring MVC

I want deploy my spring mvc application with rest to docker. I created tomcat based image with this dockerfile:

FROM tomcat:8.5.31-jre8
ADD  /target/my-app.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
EXPOSE 8080

and my rest controller:

@RestController
@RequestMapping("")
public class IndexController {
    @RequestMapping(path="", method = RequestMethod.GET)
    public String[] findAll() {
        return new String[] {"sdfsdsdf", "sfsdfsff", "fdsfsfsfdfffdsfsdf"};
    }
}

When I run my app from local Tomcat with my IDE, it works and when I go to localhost:8080 I got controller response, but now when I create image and run it when I go to localhost:8080 in browser I can see tomcat homepage, not my endpoint response. Can you tell me what I do wrong?

Here is command which I use to create and run image:

docker build -t myapp .

and

docker run -d  -p 8080:8080  --name mydockerapp myapp

I am using Win10. Thanks for answers.

Upvotes: 0

Views: 3300

Answers (1)

Ken Chan
Ken Chan

Reputation: 90467

It seems that you are deploying your web app to the root context path in the local tomcat. However, the tomcat in the docker images already have a welcome page app deployed to the root context path.

What you need to do is to delete this welcome page app and rename your app to ROOT.war , then deploy it.

FROM tomcat:8.5.31-jre8
RUN rm -rvf /usr/local/tomcat/webapps/ROOT
ADD  /target/my-app.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
EXPOSE 8080

Upvotes: 2

Related Questions