In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly describes how to use Docker to deploy Spring Boot projects, the article is very detailed, has a certain reference value, interested friends must read!
Create a simple springboot project
In pom.xml, use Spring Boot 2.2.10 dependencies
org.springframework.boot spring-boot-starter-parent 2.2.10.RELEASE II. Add web and test dependencies
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test III. Create a DockerController with a hello() method that returns: hello,nihao
@RestControllerpublic class DockerController { @RequestMapping("/hello") public String hello() { return "hello,nihao"; }} IV. Start-up class
@SpringBootApplicationpublic class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); }}
After adding, start the project. After successful startup, visit http://localhost:8080/hello, and the page returns hello,nihao, indicating that the Spring Boot project configuration is normal.
Deploying Spring Boot Projects with Docker
First, the project into jar package, copy to the server, test it
[root@jiangwang springbootDemo]# lsdemo-0.0.1-SNAPSHOT.jar Dockerfile[root@jiangwang springbootDemo]# java -jar demo-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.10.RELEASE)2021-03-18 14:49:18.241 INFO 12886 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on jiangwang with PID 12886 (/home/springbootDemo/demo-0.0.1-SNAPSHOT.jar started by root in /home/springbootDemo)2021-03-18 14:49:18.244 INFO 12886 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default2021-03-18 14:49:19.924 INFO 12886 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2021-03-18 14:49:19.938 INFO 12886 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2021-03-18 14:49:19.938 INFO 12886 --- [ 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]. [localhost]. [/] : Initializing Spring embedded WebApplicationContext2021-03-18 14:49:20.014 INFO 12886 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1657 ms2021-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 context path ''2021-03-18 14:49:20.523 INFO 12886 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.899 seconds (JVM running for 3.369) Second, after seeing the Spring Boot startup log, it shows that there is no problem with the environment configuration. Edit the Dockerfile:
FROM java:8COPY *.jar /app.jarCMD ["--server.port=8080"]EXPOSE 8080ENTRYPOINT ["java","-jar","/app.jar"] III. Next, we use Dockerfile to build the image:
##Build Mirror [root@jiangwang springbootDemo]# docker build -t springboot-demo .Sending build context to Docker daemon 17.72MBStep 1/5 : FROM java:8 ---> d23bdf5b1bStep 2/5 : COPY *. jar/app.jar--> f4d6aeabd3f0Step 3/5 : CMD ["--server.port=8080"] ---> Running in a6311f7cf7b5Removing intermediate container a6311f7cf7b5 ---> d8117b10cefaStep 4/5 : EXPOSE 8080 ---> Running in ae180be637bbRemoving intermediate container ae180be637bb ---> f16702c75ab6Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"] ---> Running in fafa00625666Removing intermediate container fafa00625666 ---> d4c3e225699dSuccessfully built d4c3e225699dSuccessfully tagged springboot-demo: The latest four, running mirror:
#running mirror [root@jiangwang springbootDemo]# docker run -d -p 39005:8080 --name my-springboot springboot-demo7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4[root@jiangwang springbootDemo]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7ac35852cb91 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/hellohello,nihao[root@jiangwang springbootDemo]#V. Enter the external network URL in your browser to visit:
Here, your extranet port 39005 should be opened first. You can go to the security group settings.
Description Successful deployment of Spring Boot project using Docker!
That's all for the article "How to deploy Spring Boot projects with Docker." Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.