Art is long, life is long

Create a simple SpringBoot project

One, in thepom.xml, using Spring Boot 2.2.10 dependency

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.210..RELEASE</version>
</parent>
Copy the code

Add Web and test dependencies

<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>Copy the code

Create a DockerControllerhello()Method that returns hello,nihao when accessed

@RestController
public class DockerController {
	
    @RequestMapping("/hello")
    public String hello(a) {
        return "hello,nihao"; }}Copy the code

4. Startup classes

@SpringBootApplication
public class DockerApplication {

	public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); }}Copy the code

Add after the start, start after a successful browser visit: http://localhost:8080/hello, page returns: hello, nihao, explain the Spring Boot project configuration is normal.

Deploy the Spring Boot project using Docker

First, put the project into a JAR package, copy to the server, test

[root@jiangwang springbootDemo]# ls demo-0.0.1- snapshot. jar Dockerfile [root@jiangwang springbootDemo]# Java -jar Demo - 0.0.1 - the SNAPSHOT. Jar. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |)))) 'there comes | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.10. RELEASE) the 2021-03-18 14:49:18. 241 INFO 12886 - [the main] com. Example. Demo. DemoApplication: Starting DemoApplication v0.0.1 - the SNAPSHOT on jiangwang with PID 12886 (/ home/springbootDemo/demo - 0.0.1 - the SNAPSHOT. The jar Started by root in/home/springbootDemo) 14:49:18 2021-03-18. 12886-244 the INFO [main] com. Example. Demo. DemoApplication  : No active profile set, falling back to default profiles: The default 14:49:19 2021-03-18. 12886-924 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat initialized with port(s): 8080 (HTTP) 14:49:19 2021-03-18. 12886-938 the INFO [main] o.a pache, catalina. Core. StandardService: Starting the service [Tomcat] 2021-03-18 14:49:19. 938 INFO 12886 - [the main] org. Apache. Catalina. Core. StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.38] 2021-03-18 14:49:20.013 INFO 12886 -- [main] O.A.C.C.C. [Tomcat].[/] : Initializing Spring Embedded WebApplicationContext 2021-03-18 14:49:20.014 INFO 12886 -- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: Initialization Completed in 1657 MS 2021-03-18 14:49:20.321 INFO 12886 -- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-03-18 14:49:20.520 INFO 12886 -- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (HTTP) with the context path '2021-03-18 14:49:20. 12886-523 the INFO [main] com. Example. Demo. DemoApplication: Started DemoApplication in 2.899 seconds (JVM running for 3.369)Copy the code

/ / Dockerfile = > Spring Boot = >

FROM java:8
COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]
Copy the code

Dockerfile is used to build the image.

## build mirror
[root@jiangwang springbootDemo]# docker build -t springboot-demo .
Sending build context to Docker daemon  17.72MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> f4d6aeabd3f0
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in a6311f7cf7b5
Removing intermediate container a6311f7cf7b5
 ---> d8117b10cefa
Step 4/5 : EXPOSE 8080
 ---> Running in ae180be637bb
Removing intermediate container ae180be637bb
 ---> f16702c75ab6
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in fafa00625666
Removing intermediate container fafa00625666
 ---> d4c3e225699d
Successfully built d4c3e225699d
Successfully tagged springboot-demo:latest
Copy the code

4. Run the mirror:

#Run the mirror[root@jiangwang springbootDemo]# docker run -d -p 39005:8080 --name my-springboot springboot-demo 7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4 [root@jiangwang springbootDemo]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7AC35852CB91 Springboot -demo "java-jar /app.jar..." 33 seconds ago Up 32 seconds 0.0.0.0:39005->8080/ TCP my-springboot [root@jiangwang springbootDemo]# curl localhost:39005/hello hello,nihao[root@jiangwang springbootDemo]#Copy the code

Five, the browser to enter the Internet url to visit:

Here your extranet port 39005 should be opened first, you can go to the security group Settings

Successful deployment of the Spring Boot project using Docker!