Dockerize Spring MVC Application

Supun Sasanka
2 min readMar 8, 2019

As you know Spring MVC is gradually being replaced by Spring Boot. But there may be some existing applications that you may want to dockerize. So in order to do this I’m going to describe about two approaches that we can take.

Approach 1

First we need to create a file named “Dockerfile” in the root of the project with the following content

FROM tomcat:8.0.20-jre8
COPY /target/myApp.war /usr/local/tomcat/webapps/

The first line suggests that get the tomcat Image from Docker hub and from the second line we are copying our war file into the webapps folder in the image.

Next we can just build the docker image and run it using the following commands

docker build -t imageName:tag .
docker run -p 9000:8080 imageName:tag

After running it we can test it by sending a request to “localhost:9000/{app-endpoint}”

Only catch using this method is you have to build the war file before creating the docker image. Not so convenient is it ? Let’s take a look at the next approach.

Approach 2

In this approach we are going to use docker-maven-plugin to build the docker image as a part of maven build process.

--

--