如何使用Docker部署Spring Boot项目
导读:本文共2785字符,通常情况下阅读需要9分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 创建一个简单的springboot项目一、在 pom.xml 中 ,使用 Spring Boot 2.2.10 相关依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</ar... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。创建一个简单的springboot项目
一、在 pom.xml 中 ,使用 Spring Boot 2.2.10 相关依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.10.RELEASE</version></parent>
二、添加 web 和测试依赖
<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>
三、创建一个 DockerController,在其中有一个hello()方法,访问时返回:hello,nihao
@RestControllerpublicclassDockerController{@RequestMapping("/hello")publicStringhello(){return"hello,nihao";}}
四、启动类
@SpringBootApplicationpublicclassDockerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DockerApplication.class,args);}}
添加完毕后启动项目,启动成功后浏览器访问:http://localhost:8080/hello,页面返回:hello,nihao,说明 Spring Boot 项目配置正常。
使用 Docker 部署 Spring Boot 项目
一、将项目打成jar包,拷贝到服务器上,测试一下
[root@jiangwangspringbootDemo]#lsdemo-0.0.1-SNAPSHOT.jarDockerfile[root@jiangwangspringbootDemo]#java-jardemo-0.0.1-SNAPSHOT.jar._________/\\/___'_____(_)______\\\\(()\___|'_|'_||'_\/_`|\\\\\\/___)||_)|||||||(_||))))'|____|.__|_||_|_||_\__,|////=========|_|==============|___/=/_/_/_/::SpringBoot::(v2.2.10.RELEASE)2021-03-1814:49:18.241INFO12886---[main]com.example.demo.DemoApplication:StartingDemoApplicationv0.0.1-SNAPSHOTonjiangwangwithPID12886(/home/springbootDemo/demo-0.0.1-SNAPSHOT.jarstartedbyrootin/home/springbootDemo)2021-03-1814:49:18.244INFO12886---[main]com.example.demo.DemoApplication:Noactiveprofileset,fallingbacktodefaultprofiles:default2021-03-1814:49:19.924INFO12886---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcatinitializedwithport(s):8080(http)2021-03-1814:49:19.938INFO12886---[main]o.apache.catalina.core.StandardService:Startingservice[Tomcat]2021-03-1814:49:19.938INFO12886---[main]org.apache.catalina.core.StandardEngine:StartingServletengine:[ApacheTomcat/9.0.38]2021-03-1814:49:20.013INFO12886---[main]o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringembeddedWebApplicationContext2021-03-1814:49:20.014INFO12886---[main]w.s.c.ServletWebServerApplicationContext:RootWebApplicationContext:initializationcompletedin1657ms2021-03-1814:49:20.321INFO12886---[main]o.s.s.concurrent.ThreadPoolTaskExecutor:InitializingExecutorService'applicationTaskExecutor'2021-03-1814:49:20.520INFO12886---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcatstartedonport(s):8080(http)withcontextpath''2021-03-1814:49:20.523INFO12886---[main]com.example.demo.DemoApplication:StartedDemoApplicationin2.899seconds(JVMrunningfor3.369)
二、看到 Spring Boot 的启动日志后表明环境配置没有问题,编辑Dockerfile文件:
FROMjava:8COPY*.jar/app.jarCMD["--server.port=8080"]EXPOSE8080ENTRYPOINT["java","-jar","/app.jar"]
三、接下来我们使用 Dockerfile 构建镜像:
##构建镜像[root@jiangwangspringbootDemo]#dockerbuild-tspringboot-demo.SendingbuildcontexttoDockerdaemon17.72MBStep1/5:FROMjava:8--->d23bdf5b1b1bStep2/5:COPY*.jar/app.jar--->f4d6aeabd3f0Step3/5:CMD["--server.port=8080"]--->Runningina6311f7cf7b5Removingintermediatecontainera6311f7cf7b5--->d8117b10cefaStep4/5:EXPOSE8080--->Runninginae180be637bbRemovingintermediatecontainerae180be637bb--->f16702c75ab6Step5/5:ENTRYPOINT["java","-jar","/app.jar"]--->Runninginfafa00625666Removingintermediatecontainerfafa00625666--->d4c3e225699dSuccessfullybuiltd4c3e225699dSuccessfullytaggedspringboot-demo:latest
四、运行镜像:
#运行镜像[root@jiangwangspringbootDemo]#dockerrun-d-p39005:8080--namemy-springbootspringboot-demo7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4[root@jiangwangspringbootDemo]#dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES7ac35852cb91springboot-demo"java-jar/app.jar…"33secondsagoUp32seconds0.0.0.0:39005->8080/tcpmy-springboot[root@jiangwangspringbootDemo]#curllocalhost:39005/hellohello,nihao[root@jiangwangspringbootDemo]#
五、浏览器输入外网网址访问一下:
这里你的外网39005端口首先要开放了,可以去安全组设置
说明使用 Docker 部署 Spring Boot 项目成功!
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
如何使用Docker部署Spring Boot项目的详细内容,希望对您有所帮助,信息来源于网络。