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
While looking for ways to achieve this. I found this awesome maven plugin called Dockerfile Maven.
01. Add Dockerfile Maven Plugin to pom file
What you’ve to do is add the following plugin section in your pom.xml file under <Project><build><plugins>.
02. Create a Dockerfile inside your project
This Dockerfile should be in the same level with pom.xml file.
.├── Dockerfile├── README.md├── pom.xml└── src
Dockerfile content would be as follow.
Just build your project with maven.
03. Build the project
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 registry.private.mydomain.com/microservice-helloworld
Happy coding !