Creating springboot project, write a simple hello world controller
The following are results of FIG.
Here is the pom.xml file.
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.lmy</groupId> 4 <artifactId>springboot-docker</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>1.5.3.RELEASE</version> 11 <relativePath /> 12 </parent> 13 14 <properties> 15 <java.version>1.8</java.version> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-test</artifactId> 27 <scope>test</scope> 28 </dependency> 29 </dependencies> 30 <build> 31 <plugins> 32 <plugin> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-maven-plugin</artifactId> 35 </plugin> 36 </plugins> 37 </build> 38 </project>
Here is the directory structure.
App.java Code
package com.lmy.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
HelloController.java Code
package com.lmy.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @GetMapping("/hi/{name}") public String sayHello(@PathVariable String name){ return "hi,"+name; } }
The springboot project packaged into jar package.
I joined in pom in a maven build the plugin, right-project, select Run As Select maven build, enter the package in goals
Console appears below the word representing packed success,
This time there has been build out of a jar in the target folder.
This jar package will be copied to the same level directory dockerfile file.
Dockerfile Code
FROM java:8
ENV REFRESHED_AT 2019-07-25
ADD springboot-docker-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Run the following command to create a mirror
sudo docker build -t lmy / springboot_v1.
Run the following command to create run container,
sudo Docker RUN -d -p 8085: 8080 LMY / springboot_v1
Below we can see already run successful.