Dockerizing Spring Boot App
By ski11up.com
Problem
Typically in an application development context, the developer has completed the code for the feature in the sprint, and he wants QA to look into it in the same sprint to find the bugs if there are any. The developer requested DevOps to deploy the application on the QA environment, but the DevOps has other high-priority work. QA started deploying the application to the test environment but misses some configuration and later it took more than a day to fix all issues. Crucial time is lost doing nonproductive things during the sprint.
Solution
It is always a good practice to use dockerized environment for testing. We should Dockerize the app in a container and pass it on to the QA for further testing. Or he can build the image on a local machine and start developing a test automation script.
Dokerfile
Use the below Dockerfile for building the spring-boot application and deploy on tomcat-8.
1
2
3
4
5
6
7
8
9
10
FROM tomcat:8.5.47-jdk8-openjdk (1)
MAINTAINER Jagdeep Jain "jagdeep.jain@gmail.com"
EXPOSE 8080 (2)
RUN rm -rf ./webapps/* (3)
COPY target/app.war /usr/local/tomcat/webapps/app.war (4)
ENV JAVA_OPTS="-Dspring.profiles.active=dev" (5)
CMD ["catalina.sh", "run"] (6)
1 | we need to pull the tomcat image with JDK 8 |
2 | we need to expose port 8080 |
3 | delete all other web apps or .war files |
4 | copying the war file provided by the developer |
5 | set spring boot profile |
6 | start the tomcat server |
Feel free to experiment with different versions of tomcat and jdk.
Build
We need to build the image based on the latest war file provided by the developer, below is the command to build the same.
1
docker image build -t sa .
Run Container
Once the image is ready, we need to run the container which will host the web application for further testing.
sa is the name of the image.
1
docker run -p 8080:8080 -t sa
Look at -p, which is required for running the application. This is mapping the exposed port with an external port.
Open any browser and enter http://localhost:8080/app and you can use the app, write your test script, execute it.
Test Automation
The same can be used in an automation testing environment while building the application and running tests against the URL.
Source Code
The complete source is located at spring-boot-app-container feel free to either git fork
, git clone
or download this repository.