Dockerize Spring MVC Application

Dockerize Spring MVC Application

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.

So first thing we have to do is add the following configuration to the pom.xml file under the build section.

<plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <executions>
      <execution>
         <phase>install</phase>
         <goals>
            <goal>build</goal>
         </goals>
      </execution>
   </executions>
   <configuration>
      <imageName>ImageName:tag</imageName>
<baseImage>tomcat:8.0.20-jre8</baseImage> <resources> <resource> <targetPath>/usr/local/tomcat/webapps/</targetPath> <directory>target/</directory> <include>myApp.war</include> </resource> </resources> </configuration> </plugin>

Documentation : https://github.com/spotify/docker-maven-plugin

What we have done here is instead of using the Dockerfile we specified the necessary commands directly in the pom.xml. Now in order to build the image you can use

mvn clean package docker:build

or

mvn clean install

Note that we didn’t use Dockerfile here. But there is a way to use the Dockerfile in this process. In order to do that change the configuration as follows

<plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <executions>
      <execution>
         <phase>install</phase>
         <goals>
            <goal>build</goal>
         </goals>
      </execution>
   </executions>
   <configuration>
      <imageName>ImageName:tag</imageName>
      <dockerDirectory>.</dockerDirectory>
      <resources>
         <resource>
            <targetPath>/usr/local/tomcat/webapps/</targetPath>
            <directory>target/</directory>
            <include>myApp.war</include>
         </resource>
      </resources>
   </configuration>
</plugin>

Note the “dockerDirectory” property, which is the directory Dockerfile resides in.

That’s it.

Thank you for taking the time to read this.

Leave a Comment

%d bloggers like this: