您好,登錄后才能下訂單哦!
使用Dockerfile怎么部署一個SpringBoot項目?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1、創建一個SpringBooot項目并且打成jar包
2、在Linux中創建一個文件夾,來做docker測試
[root@izwz90lvzs7171wgdhul8az ~]# mkdir /root/docker_test
3、將jar包上傳到Linux中
創建存放jar包的文件夾
[root@izwz90lvzs7171wgdhul8az docker_test]# mkdir /root/docker_test/jar
然后利用XShell上傳jar包到上面的文件夾中
4、編寫Dockerfile文件
# 基于java鏡像創建新鏡像 FROM java:8 # 作者 MAINTAINER Howinfun # 將jar包添加到容器中并更名為app.jar ADD jar/app.jar /root/docker_test/app.jar # 運行jar包 ENTRYPOINT ["nohup","java","-jar","/root/docker_test/app.jar","&"]
注意:ADD 、 COPY 指令用法一樣,唯一不同的是 ADD 支持將歸檔文件(tar, gzip, bzip2, etc)做提取和解壓操作。還有需要注意的是,COPY 指令需要復制的目錄一定要放在 Dockerfile 文件的同級目錄下。
5、制作鏡像
[root@izwz90lvzs7171wgdhul8az docker_test]# docker build -t sbdemo .
命令參數:
-t:指定新鏡像名
.:表示Dockfile在當前路徑
如果我們的 Dockerfile 文件路徑不在這個目錄下,或者有另外的文件名,我們可以通過 -f 選項單獨給出 Dockerfile 文件的路徑
[root@izwz90lvzs7171wgdhul8az docker_test]# docker build -t sbdemo -f /root/docker_test/Dockerfile /root/docker_test/
命令參數:
-f:第一個參數是Dockerfile的路徑 第二個參數是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
命令參數:
-d:后臺運行
-p:公開指定端口號
--name:給容器命名
啟動后可通過docker ps查看正在運行的容器:
[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工具去訪問部署在容器中的應用接口。
看完上述內容,你們掌握使用Dockerfile怎么部署一個SpringBoot項目的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。