Thursday, 11 June 2020

Spring Boot With Docker

We have seen more definition about the containerization and its time for us to see some original work in action.
We will See in this post how to create a docker image and run from it. Of course, it is available in the spring site as a tutorial. I am providing here my version of achieving it.


The First you have to do with your application is to create the jar file of the application, this can be achieved by executing the below commands.

Prerequisite.

1.       If your application is maven based you have to execute the goal as” mvn:package”.

2.    Once you execute the package command it tries to create the jar and run the unit test cases, if you want not to execute the test cases then you can pass the following along with the goal. -Dmaven.test.skip=true

3.       Once the jar is generated you can check your application is running by calling the jar file.  Java -jar springbootdemo-0.0.1-SNAPSHOT.jar once you execute this, you can see the application started running.

4.       Download the docker from the website. https://docs.docker.com/installation/#installation and install it in the local. Makesure you are able to run the docker commands after installation.

Containerization.

1.       Create a file named “Dockerfile” with the following contents.

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

2   You can run the “docker build -t employeeservice . “ to create the tag.

This Dockerfile is very simple, but that’s all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build will create a spring user and a spring group to run the application. It will then COPY the project JAR file into the container as "app.jar" that will be executed in the ENTRYPOINT. The array form of the Dockerfile ENTRYPOINT is used so that there is no shell wrapping the java process.

you can run the application using the following command.

docker run -p 7010:7010 employeeservice

That’s it now your application starts running from the docker image generated. Execute this from the directory where you have the docker file. On executing the build it will download the required and run the application locally.

That’s all Congrats on your first docker app.


No comments:
Write comments