Spring boot with Docker

INTRODUCTION TO SPRING BOOT
1. Spring boot applications main approach is reducing the time to design real-time applications.
2. In spring projects manually write the all XML configuration and server settings, in the spring-boot application we can avoid all these things.
3. The main advantage is spring boot built with the inbuilt server.

INTRODUCTION TO DOCKER
1. Docker is an open source platform for developing, shipping, and running applications.
2. Docker enables you to separate your applications for your infrastructure.
3. Using docker we can manage the applications and deploy into test prod machines easily.
4.By using the docker deploying code quickly.

ENVIRONMENT SETUP 
1. Download and install the latest sts:https://spring.io/tools3/sts/all  or eclipse:https://www.eclipse.org/downloads/ (If already installed please ignore this step).
2. Download and install the docker toolbox:https://docs.docker.com/docker-for-windows/install/ (If already installed please ignore this step).
3.If docker is successfully installed or not to check docker pull hello-world (It will pull the latest docker image form docker hub).
Pulling image
To check the docker version use the following command: docker -version
Version of docker
4. After that, to check the image is downloaded or not docker images (it will show all images in your docker engine).
Docker Images
5.For run the hello-world image use the following command: docker run hello-world


    It will show a message like: Hello from Docker!

   This message shows that your installation appears to be working correctly.

6. Or else you can directly run below command: docker run hello-world (Instead of docker pull...).
Running application
7. To know the running containers use the following command: docker ps -a it will show the currently running containers on the docker engine.
8. To stop the Container use the following command: docker stop container_id.
9. To remove the container using the following command: docker rm container_id.
Remove Container
10. To remove the image use the following the command: docker rmi image_name
Remove Image


CREATING SPRING BOOT REST SERVICE

1. Open the Eclipse or sts and create the maven project.
    Open STS (Spring Tool Suite)-> File->New->Maven Project.

STS_Dashboard
Creating Maven Project
2. Tick Create a simple project (skip archetype selection) check box-> click Next.
    Provide Group Id (it's your package), Artifact Id (project name) and click Finish.
Project
3. Now you will see a Maven project in your workspace, something like.
Structure
4. Here is the pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.janardhan</groupId>
 <artifactId>SpringSample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>SpringBoot</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <docker.image.prefix>spring-boot-tutorialspoint</docker.image.prefix>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
  <finalName>SpringSample</finalName>
 </build> 
</project>
5. I have added spring-boot-starter-parent, spring-boot-starter-web dependencies this is an existing project given by spring team which contains Spring Boot supporting configuration data
spring-boot-starter-web this indicate spring application is the child of the parent.
6. Here is the main class.
package com.janardhan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SpringSample {

 public static void main(String[] args) {

  SpringApplication.run(SpringSample.class, args);

 }

}
7. Run the main class as a Java application or spring boot app, by default it will run on 8080 port.
Running
8. Next, Create the Controller package under source package.
package
9. Then create the sample java RestController class.
@RestController: tells Spring Boot to consider this class as a REST controller
@RequestMapping: Response to HTTP path requests.
package com.janardhan.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringSampleController {

 @RequestMapping("/")
 public String Body() {
  return "Welcome to Spring Boot Sample Applicaton";
 }
 
 @RequestMapping("/hello")
 public String hello()
 {
  return "Welcome to sample Rest service";
 }
}
CREATING THE DOCKER FILE
1. It is the main part of your docker image, based on this file application will package to image.
2. Docker files have no extensions.
3. Use the predefined commands to package the application provided by docker.
4.FROM, COPY, CMD, MAIN, MAIN-CLASS, etc.... these are the commands.
FROM openjdk:8
ADD  target/SpringSample.jar SpringSample.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","SpringSample.jar"]

Explanation
:

FROM: Define the base image used to start the build process.for java applications build to use OpenJDK:8
ADD: Copy the source file into the container. Here I Added the targeted the jar file and second argument aa s jar file name as which you want to name it.
EXPOSE: Expose the port number to the web application. binding the port to application.
EN TRYPOINT: Set a default application to be used every time a container is created with the image.

COMPILING APPLICATION
For compiling the application use the following maven command: mvn clean install (It will clean all directories and download the dependencies provided on pom.xml file.
Compiling the application

After compiling the application jar file created in the target folder.
Now place the docker file on project directory like bow screenshot.
Docker file
Here is docker file for sample spring-boot rest service.
FROM openjdk:8
ADD  target/SpringSample.jar SpringSample.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","SpringSample.jar"]

CREATING DOCKER IMAGE USING DOCKER FILE
For creating the docker image use the following command: docker build -f Dockerfile -t spring sample
NoteTag name should lowercase only.
Dockerfile
After creating the docker images to check image is created or not the following command is: docker images
Docker Image

RUNNING APPLICATION

To run the docker file use the following command: docker run -p 9090:9090 -t spring sample
Running application
Now access the spring boot application which we created docker image previously.
To know the docker machine ip use the following command: docker-machine IP
application


Application 1

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

0 Response to "Spring boot with Docker "

Post a Comment