Deploying Spring Boot War in Tomcat running in Docker

Vicky AV
2 min readMar 5, 2020
Steps to Dockerize Spring Boot Application

If you want to run SpringBoot application as JAR file with Docker, check my other post — Running Spring Boot JAR with Docker

First things First — Lets create a WAR file

As you know, Spring Boot based application by default creates executable JAR. We need to configure a bit to make Spring Boot create a WAR file as output.

  1. Change packing to war in pom.xml
<packaging>war</packaging>

2. Configure a custom name for your war file by setting finalName property in spring-boot-maven-plugin

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>sample</finalName>
</configuration>
</plugin>

Remember : In order for your application to run in standalone tomcat, your code needs little tweaks. Please refer it here

https://github.com/iamvickyav/Developer-Tips/blob/master/Deploy-SpringBootApp-In-Tomcat.md

Time to Dockerize our Application

Lets start with creating a Dockerfile

FROM tomcat:latest
ADD target/sample.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]

DockerFile demystified

FROM — Base image on top of which our custom image is to be build

ADD — Copy war file into webapps folder inside Docker

EXPOSE — Expose port 8080 outside Docker container

CMD — The command to be executed when the Docker image is run

Here is our docker-compose.yml file

Now lets run the following command

> docker-compose up —-build

Since we specified DOT (.) in build, docker-compose will search for Dockerfile in current directory & will build the image with name specified in image property (in this case its test-img)

Now your application is running in Tomcat inside Docker !!

Source code — https://github.com/iamvickyav/SpringBoot-Docker-Tomcat

Next :

Step by Step instruction on how to build docker image and start the Docker container via Jenkins

https://medium.com/@iamvickyav/build-up-docker-image-via-jenkins-in-local-43b4c5ab4a2b

--

--