Spring boot docker Integration – JavaDream

Home / Spring boot docker Integration – JavaDream

In this post we will see Spring boot docker Integration. We often hear the word that this project work on my system perfectly i don’t know why this is not working in your system. This is the common issue between developer and tester.

And one more thing that when you develop your project and send it to DevOps team for deployment you see that this team setup the server before deploy your application like they install java version on which you develope your project, Tomcat server, Database Server then they deploy your application.

It is very old feture now days and become a headache for devops team to setup the complete server before deployment. What we can do to reduce this task so here we go here we use Docker.

Docker is a container that contain every dependency of our project and make a image and we share this image to everyone. Now we don’t need to worry about anything we just use this image and run our application. Before use docker you have to download it.

To download docker you can go to official website of docker

https://docs.docker.com/get-docker/

For docker spring boot application we have to define a file with name Dockerfile. And in this file we define some syntax to make a docker image.

We have to follow the below steps for Spring boot docker Integration or to make a docker image of spring boot application and they are:

  1. Create a Simple web spring boot project.
  2. Add required dependency in pom.xml file.
  3. Define Name of your jar in your pom.xml file.
  4. Make Jar of your spring boot Project.
  5. Make a docker file with name Dockerfile.
  6. Run docker command to make docker image.
  7. Run this docker image.

Project description: In this docker spring boot project we simplly make a endPoint and access this endpoint using docker Image.

Project Directoy:

spring boot docker

1- Create a simple web Spring boot project.

2- Add required dependency in pom.xml file.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

3- Define Name of your jar in your pom.xml file.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>spring-docker</finalName>
</build>

In above syntax see the tag <finalName> this tag is used to define the name of your spring boot jar. When you build your spring boot project you will see the jar name same as that you give in between <finalName> tag.

4- Now make a build of your project using maven install command.

5- Make a docker file with name Dockerfile

FROM java:8
EXPOSE 8080
ADD target/spring-docker.jar spring-docker.jar
ENTRYPOINT ["java","-jar","/spring-docker.jar"]

In above syntax:

From – This is used to define that our project is build on java8 and docker will install this image from dockerhub if it is not present.

EXPOSE – This is used to define that we use 8080 port in docker container.

ADD – This is used to define or jar path. we know that our jar file build inside target folder and jar name is same as that we deifine between <finalName> tag.

ENTRYPOINT – This is used to define the command that run our spring boot application. We know that to run java jar we use java -jar <jarName> command.

6- Run docker command to make docker image

Now open your project in terminal and write the below command to make a docker image.

sudo docker build -t spring-docker.jar .

If you run the above command first time it will take some time to download required images. After run the above command you see the output like.

docker spring boot

To see the list of images in docker we use below command.

sudo docker images
spring boot microservices with docker

7- Run this docker image.

sudo docker run -p 9090:8080 spring-docker.jar

Above command is used to run the docker Image. Here we define 9090:8080 this mean we use 8080 as a docker container port and 9090 as our application port. Means to access your application we use http://localhost:9090/ URL. After run above command you will see the output like below:

spring boot docker tutorial

Now go to your web browser and access your endpoint.

spring boot docker

Github URLhttps://github.com/vasurajput/Spring-Boot-Web/tree/master/SpringbootDocker

You may also like:

spring boot jwt authentication example

Case Statement in SQL example

Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *