Continuous Integration and Delivery for Maven projects with Jenkins and Docker.

In this article we look at continuous integration for Maven projects using Jenkins and Docker. The project from my previous post Multiple Databases in Spring Boot will be built and deployed and run by Jenkins. The codebase for Multiple Databases in Spring Boot is https://github.com/gabrieljeremiahcampbell/multipledatabases

First I will discuss the methodology I use to build the Maven project and create the Docker image. There are normally two approaches:

1. Using a maven plugin for Docker that can run the Docker commands as tasks and goals.

2.Let Docker build the Maven project.

The problem I see in the first methodology is that it can create a dependency of ensuring that your Maven plugins are up to date especially if the Docker API changes. I choose to go with the second option because this way we have decoupled Maven from Docker. It is a type of Inversion Of Control(IoC) – Maven should not know about Docker as it’s focus is the project. In other words Docker is one of many environments that can run our app. If we had other platforms to run that means our POM would have to have plugins for everyone. So I will build the project using Docker.

In our IDE we open our project, we create the following Dockerfile:

image

This Dockerfile is a multistage build. First I use a maven container to build my image, maven3.5.2-jdk-8-apline. 

The important aspect to note is that Docker is running the Maven commands for building and packaging the application.

After this I use an openjdk:8-jdk-apline image to build to create the container for my project. Lastly I expose the port 8080, that my application will listen on.

In Jenkins we create the build plan with the following steps. I list all the screenshots so you can see the commands involved:

image

image

image

image

The last command prune will delete any temporal containers used in the multistage build and created by Docker. It is like a clean up operation. If our container fails to start and is in error it would also be removed. This reduces the manual effort of cleaning up the container and image registries in Docker. However it should be used with caution.

The Dockefile is the GitHub repos link shared above.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s