這篇文章主要介紹了Docker如何部署Spring-boot項目的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Docker如何部署Spring-boot項目文章都會有所收獲,下面我們一起來看看吧。
宿松ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
一、基礎(chǔ)spring-boot快速啟動
1.1 快速啟動 pom.xml加入如下依賴
org.springframework.boot spring-boot-starter-parent 2.0.5.release 1.8 utf-8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test spring-docker org.springframework.boot spring-boot-maven-plugin
spring-boot啟動類
@springbootapplication public class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); } }
測試api
@restcontroller public class dockerstarterapi { @getmapping("/api/docker/hello") public string hello() { return "hello docker"; } }
配置啟動配置文件 application.yml
server: port: 9090 # 為了展示效果, 這里改了默認端口8080
檢查spring啟動
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v2.0.2.release) ... 2018-12-17 17:26:13.385 info 48740 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2018-12-17 17:26:13.448 info 48740 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 9090 (http) with context path '' 2018-12-17 17:26:13.453 info 48740 --- [ main] pers.study.docker.dockerapplication : started dockerapplication in 1.982 seconds (jvm running for 2.602)
檢查api是否生效
$ curl -xget 'http://localhost:9090/api/docker/hello' hello docker
瀏覽器檢查
http://localhost:9090/api/docker/hello
1.2 打包啟動
項目打包
完成上面步驟之后,執(zhí)行打包命令:
$ mvn clean -u -dmaven.test.skip compile package
因為上面的pom文件里面定義了 finalname
,所以在這里會看到編譯打包之后 target
目錄下會生成 spring-docker.jar
spring-docker
測試運行
$ java -jar target/spring-docker.jar
不出意外(有問題留言~)運行結(jié)果同上并檢查api是否生效即可.
二、docker快速安裝
接下來開始準備docker
安裝
檢查安裝、查看幫助
$ docker --version docker version 18.06.0-ce, build 0ffa825 $ docker --help usage: docker [options] command a self-sufficient runtime for containers ...
鏡像加速
三、配置spring-boot + docker
pom.xml 添加docker plugin
springboot com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix}/${project.build.finalname} src/main/docker / ${project.build.directory} ${project.build.finalname}.jar
創(chuàng)建 dockerfile
文件
根據(jù)上面 pom.xml
文件配置
,這里配置了docker配置文件的目錄,所以需要再 src/main
下面創(chuàng)建docker文件夾,同時創(chuàng)建 dockerfile
文件。
目錄機構(gòu)如圖:
docker配置文件結(jié)構(gòu).png
編輯 dockerfile
from openjdk:8-jdk-alpine volume /tmp add spring-docker.jar app.jar entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
from 表示以java8為基礎(chǔ)鏡像
volume 表示掛載目錄
add 拷貝打包文件并重命名為 app.jar
entrypoint 根據(jù)下面的官方文檔解釋大致是為了縮短tomcat啟動時間而添加的一個系統(tǒng)屬性。
we added a
volume
pointing to/tmp
because that is where a spring boot application creates working directories for tomcat by default. the effect is to create a temporary file on your host under/var/lib/docker
and link it to the container under/tmp
. this step is optional for the simple app that we wrote here but can be necessary for other spring boot applications if they need to actually write in the filesystem.to reduce tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy. this is not necessary with more recent versions of spring boot, if you use the "standard" version of tomcat (or any other web server).
配置完成!
四、docker啟動spring-boot
進入 module
執(zhí)行:
$ mvn package docker:build [info] scanning for projects... ... ---> running in e1f8aba72bdf removing intermediate container e1f8aba72bdf ---> 36a61c09f09a progressmessage{id=null, status=null, stream=null, error=null, progress=null, progressdetail=null} successfully built 36a61c09f09a successfully tagged springboot/spring-docker:latest [info] built springboot/spring-docker [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 6.367 s [info] finished at: 2018-12-17t20:48:21+08:00 [info] ------------------------------------------------------------------------
查看鏡像
$ docker images repository tag image id created size springboot/spring-docker latest 36a61c09f09a 2 minutes ago 123mb
運行鏡像
$ docker run -p 9090:9090 -t springboot/spring-docker . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v2.0.2.release) 2018-12-17 12:53:21.502 info 1 --- [ main] pers.study.docker.dockerapplication : starting dockerapplication v1.0-snapshot on 94991c04be5d with pid 1 (/app.jar started by root in /) 2018-12-17 12:53:21.509 info 1 --- [ main] pers.study.docker.dockerapplication : no active profile set, falling back to default profiles: default ··· 2018-12-17 12:53:25.255 info 1 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2018-12-17 12:53:25.337 info 1 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 9090 (http) with context path '' 2018-12-17 12:53:25.353 info 1 --- [ main] pers.study.docker.dockerapplication : started dockerapplication in 4.485 seconds (jvm running for 5.346)
查看容器
$ docker ps container id image command created status ports names 94991c04be5d springboot/spring-docker "java -djava.securit…" 53 seconds ago up 52 seconds 0.0.0.0:9090->9090/tcp quizzical_bhabha
驗證啟動,訪問api
$ curl -xget 'http://localhost:9090/api/docker/hello' hello docker
至此docker部署spring-boot搭建完成。
五、移除鏡像
停止容器
$ docker stop 94991c04be5d 94991c04be5d
刪除容器
$ docker rm 94991c04be5d 94991c04be5d
刪除鏡像
$ docker image rm springboot/spring-docker untagged: springboot/spring-docker:latest deleted: sha256:36a61c09f09ab88cfe5a05f564deb57498682f4a6f3ec01d2a8c4fdc80ac1e41 deleted: sha256:3f9aef70be6d4d43c205454d8874f10bc2f7280f70eb88cd1f04937b7965dd27 deleted: sha256:9a5800e93615bb4c5128bb36d31ec494327c01f1a9a768c1ff538badf76628b9 deleted: sha256:d9c66f907448fa9e61fd5f9267d7fcf8e1f4b52d0a20466414f2f45777261284
六、其他配置功能
添加環(huán)境屬性
$ docker run -e "spring_profiles_active=prod" -p 9090:9090 -t springbooot/spring-docker
后臺啟動運行
$ docker run -p 9090:9090 -d springboot/spring-docker
開啟容器debug 修改 dockerfile
from openjdk:8-jdk-alpine volume /tmp add spring-docker.jar app.jar env java_opts '' cmd java -djava.security.egd=file:/dev/./urandom $java_opts -jar app.jar
docker run
復(fù)制代碼 代碼如下:
$ docker run -e "java_opts=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 9090:9090 -p 5005:5005 -t springboot/spring-docker
關(guān)于“Docker如何部署Spring-boot項目”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Docker如何部署Spring-boot項目”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。