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).
To check the docker version use the following command: docker -version
4. After that, to check the image is downloaded or not docker images (it will show all images in your docker engine).
5.For run the hello-world image use the following command: docker run hello-world
6. Or else you can directly run below command: docker run hello-world (Instead of docker pull...).
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.
10. To remove the image use the following the command: docker rmi image_name
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.
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.
3. Now you will see a Maven project in your workspace, something like.
4. Here is the pom.xml
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.
7. Run the main class as a Java application or spring boot app, by default it will run on 8080 port.
8. Next, Create the Controller package under source 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.
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.
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.
After compiling the application jar file created in the target folder.
Now place the docker file on project directory like bow screenshot.
Here is docker file for sample spring-boot rest service.
CREATING DOCKER IMAGE USING DOCKER FILE
For creating the docker image use the following command: docker build -f Dockerfile -t spring sample
Note: Tag name should lowercase only.
After creating the docker images to check image is created or not the following command is: docker images
RUNNING APPLICATION
To run the docker file use the following command: docker run -p 9090:9090 -t spring sample
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
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).
To check the docker version use the following command: docker -version
4. After that, to check the image is downloaded or not docker images (it will show all images in your docker engine).
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.
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.
10. To remove the image use the following the command: docker rmi image_name
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.
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.
3. Now you will see a Maven project in your workspace, something like.
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>
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); } }
8. Next, Create the Controller package under source 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"; } }
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.
After compiling the application jar file created in the target folder.
Now place the docker file on project directory like bow screenshot.
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
Note: Tag name should lowercase only.
After creating the docker images to check image is created or not the following command is: docker images
RUNNING APPLICATION
To run the docker file use the following command: docker run -p 9090:9090 -t spring sample
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
0 Response to "Spring boot with Docker "
Post a Comment