Containerize your Microservice

At one point I wanted to host my microservices in a kubernetes cluster. So the first thing I did is trying to containerize my microservices. These micro services were written in Sprinboot. But you can do it to any maven project.

When you start the microservice in your local machine following is what you do.

java -jar my-springboot-microservice.jar

Lets containerize

While looking for ways to achieve this. I found this awesome maven plugin called Dockerfile Maven.

What you’ve to do is add the following plugin section in your pom.xml file under <Project><build><plugins>.

This Dockerfile should be in the same level with pom.xml file.

.├── Dockerfile├──├── pom.xml└── src

Dockerfile content would be as follow.

Just build your project with maven.

mvn clean install

This will do 3 things

  • Compile and build your springboot microservice to a jar file.
  • Crete a docker image with the name you have mentioned under <repository> tag in plugin configurations in pom.xml file.
  • Push the image to private docker registry mentioned in <repository> tag.

Note : if you want to disable the image pushing part then remove the element <goal>push</goal>

docker run -p 8080:8080 -t

Happy coding !