Dockerise a maven war app

Dockerise a maven war app

Using Tomcat server

Maven is build automation and dependency management tool primarily for java based applications.

Developers can easily manage and control dependencies for their projects using a centralized repository.

If you already have a webapp setup for you application. You can proceed to the dockerfile.

Rest for learning we can generate a java webapp using following commands ->

  1. mvn archetype:generate

  2. Choose 371 or search (simple-webapp-archetype) basic.

  3. Input you values for the fields like name group id etc.

  4. This will generate a basic war app

  5. Edit the pom.xml with dependencies of your choice.

  6. Run mvn clean compile package

Now we will dockerise our app maven app. We will create a basic dockerfile for this.

Defines the base image for dockerfile

FROM

RUN will help us execute any command

RUN

COPY is copying from src to dest

COPY

WORKDIR will be the directory when we go
inside the docker image the base folder will be that WORKDIR folder

WORKDIR

Do refer above explanation or feel free to comment.

Basic Dockerfile ->

FROM maven:3-jdk-11 as builder
RUN mkdir -p /build
COPY . /build
WORKDIR /build
RUN mvn clean compile package

The above dockerfile will be somewhat sufficient for a python based webapp or anyother but not for this app.

As maven doesnt get shipped with a default server like python (django, flask).

We require additional server maybe nginx or tomcat. I used a tomcat server to run it.

FROM maven:3-jdk-11 as builder
RUN mkdir -p /build
COPY . /build
WORKDIR /build
RUN mvn clean compile package

FROM tomcat:7.0
COPY --from=builder /build/target /tom/target
WORKDIR /tom/target
RUN cp api.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]

This will be one combined dockerfile it will package the mvn war app and render onto a tomcat server.
These are connected as u can see maven war app and packaged target will be copied to the tomcat server.

Hopefully if you directly place the dockerfile in your app too it might run.

Do let me know if you have any doubts or comments. Will be happy to help.