本篇內(nèi)容主要講解“怎么利用Dockerfile部署SpringBoot項目”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么利用Dockerfile部署SpringBoot項目”吧!
創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實(shí)施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元樂平做網(wǎng)站,已為上家服務(wù),為樂平各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
1、創(chuàng)建一個springbooot項目并且打成jar包
2、在linux中創(chuàng)建一個文件夾,來做docker測試
[root@izwz90lvzs7171wgdhul8az ~]# mkdir /root/docker_test
3、將jar包上傳到linux中
創(chuàng)建存放jar包的文件夾
[root@izwz90lvzs7171wgdhul8az docker_test]# mkdir /root/docker_test/jar
然后利用xshell上傳jar包到上面的文件夾中
4、編寫dockerfile文件
# 基于java鏡像創(chuàng)建新鏡像 from java:8 # 作者 maintainer howinfun # 將jar包添加到容器中并更名為app.jar add jar/app.jar /root/docker_test/app.jar # 運(yùn)行jar包 entrypoint ["nohup","java","-jar","/root/docker_test/app.jar","&"]
注意:add 、 copy 指令用法一樣,唯一不同的是 add 支持將歸檔文件(tar, gzip, bzip2, etc)做提取和解壓操作。還有需要注意的是,copy 指令需要復(fù)制的目錄一定要放在 dockerfile 文件的同級目錄下。
5、制作鏡像
[root@izwz90lvzs7171wgdhul8az docker_test]# docker build -t sbdemo .
命令參數(shù):
-t:指定新鏡像名
.:表示dockfile在當(dāng)前路徑
如果我們的 dockerfile 文件路徑不在這個目錄下,或者有另外的文件名,我們可以通過 -f 選項單獨(dú)給出 dockerfile 文件的路徑
[root@izwz90lvzs7171wgdhul8az docker_test]# docker build -t sbdemo -f /root/docker_test/dockerfile /root/docker_test/
命令參數(shù):
-f:第一個參數(shù)是dockerfile的路徑 第二個參數(shù)是dockerfile所在文件夾制作完成后通過docker images命令查看我們制作的鏡像:
[root@izwz90lvzs7171wgdhul8az docker_test]# docker images | grep sbdemo sbdemo latest 7efac46ef997 4 hours ago 686mb
6、啟動容器
[root@izwz90lvzs7171wgdhul8az docker_test]# docker run -d -p 8888:8888 --name mysbdemo sbdemo:latest
命令參數(shù):
-d:后臺運(yùn)行
-p:公開指定端口號
--name:給容器命名
啟動后可通過docker ps查看正在運(yùn)行的容器:
[root@izwz90lvzs7171wgdhul8az docker_test]# docker ps container id image command created status ports names 5096c8c7b36f sbdemo "nohup java -jar /ro?? 4 seconds ago up 2 seconds 0.0.0.0:8888->8888/tcp mysbdemo
7、查看容器啟動日志
我們可以通過 docker logs 查看指定容器的日志:
[root@izwz90lvzs7171wgdhul8az docker_test]# docker logs mysbdemo . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v2.1.6.release) 2019-10-11 02:10:46.264 info 1 --- [ main] com.hyf.databaseapplication : starting databaseapplication v0.0.1-snapshot on 6d85ac5d8751 with pid 1 (/root/docker_test/app.jar started by root in /) 2019-10-11 02:10:46.267 debug 1 --- [ main] com.hyf.databaseapplication : running with spring boot v2.1.6.release, spring v5.1.8.release 2019-10-11 02:10:46.268 info 1 --- [ main] com.hyf.databaseapplication : no active profile set, falling back to default profiles: default 2019-10-11 02:10:49.139 warn 1 --- [ main] o.m.s.mapper.classpathmapperscanner : skipping mapperfactorybean with name 'bookmapper' and 'com.hyf.mapper.bookmapper' mapperinterface. bean already defined with the same name! 2019-10-11 02:10:49.139 warn 1 --- [ main] o.m.s.mapper.classpathmapperscanner : no mybatis mapper was found in '[com.hyf]' package. please check your configuration. 2019-10-11 02:10:49.246 info 1 --- [ main] .s.d.r.c.repositoryconfigurationdelegate : multiple spring data modules found, entering strict repository configuration mode! 2019-10-11 02:10:49.257 info 1 --- [ main] .s.d.r.c.repositoryconfigurationdelegate : bootstrapping spring data repositories in default mode. 2019-10-11 02:10:49.328 info 1 --- [ main] .s.d.r.c.repositoryconfigurationdelegate : finished spring data repository scanning in 39ms. found 0 repository interfaces. 2019-10-11 02:10:50.345 info 1 --- [ main] trationdelegate$beanpostprocessorchecker : bean 'org.springframework.transaction.annotation.proxytransactionmanagementconfiguration' of type [org.springframework.transaction.annotation.proxytransactionmanagementconfiguration$$enhancerbyspringcglib$$2c6b335] is not eligible for getting processed by all beanpostprocessors (for example: not eligible for auto-proxying) 2019-10-11 02:10:51.255 info 1 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat initialized with port(s): 8888 (http) 2019-10-11 02:10:51.359 info 1 --- [ main] o.apache.catalina.core.standardservice : starting service [tomcat] 2019-10-11 02:10:51.359 info 1 --- [ main] org.apache.catalina.core.standardengine : starting servlet engine: [apache tomcat/9.0.21] 2019-10-11 02:10:51.778 info 1 --- [ main] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2019-10-11 02:10:51.779 info 1 --- [ main] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 5104 ms 2019-10-11 02:10:54.164 info 1 --- [ main] o.s.s.concurrent.threadpooltaskexecutor : initializing executorservice 'applicationtaskexecutor' 2019-10-11 02:10:56.081 info 1 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 8888 (http) with context path '' 2019-10-11 02:10:56.090 info 1 --- [ main] com.hyf.databaseapplication : started databaseapplication in 11.49 seconds (jvm running for 12.624)
8、訪問接口
容器啟動后,我們嘗試使用postman或者其他http工具去訪問部署在容器中的應(yīng)用接口。
到此,相信大家對“怎么利用Dockerfile部署SpringBoot項目”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!