您好,登錄后才能下訂單哦!
這篇文章主要講解了“Docker怎么部署springboot項目”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Docker怎么部署springboot項目”吧!
創建項目
pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.1.9.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.topcheer</groupid> <artifactid>docker</artifactid> <version>0.0.1-snapshot</version> <name>docker</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> <docker.image.prefix>topcheer</docker.image.prefix> </properties> <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> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>com.spotify</groupid> <artifactid>docker-maven-plugin</artifactid> <version>1.0.0</version> <configuration> <imagename>${docker.image.prefix}/${project.artifactid}</imagename> <dockerdirectory>src/main/docker</dockerdirectory> <resources> <resource> <targetpath>/</targetpath> <directory>${project.build.directory}</directory> <include>${project.build.finalname}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
啟動類
@springbootapplication @controller public class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); } @requestmapping("/user/find") @responsebody public object finduser(){ map<string, string > map = new hashmap<>(); map.put("name", "xdclass.net"); map.put("age","28"); return map; } }
由于harbor沒有安裝,無法把鏡像推到私有鏡像倉庫上,所以先手動執行。
mvn install,然后把dockerfile和jar包放到一起
[root@topcheer docker]# ll 總用量 16452 -rw-r--r-- 1 root root 168 10月 28 14:24 dockerfile -rw-r--r-- 1 root root 16842487 10月 28 14:17 docker.jar [root@topcheer docker]# cat dockerfile from java:8 volume /tmp add docker.jar app.jar run bash -c 'touch /app.jar' expose 8080 entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] [root@topcheer docker]#
編譯成鏡像并啟動
[root@topcheer docker]# docker build -f dockerfile -t boot . sending build context to docker daemon 16.85 mb step 1/6 : from java:8 ---> d23bdf5b1b1b step 2/6 : volume /tmp ---> running in 35037b5a7791 ---> e96b96457c78 removing intermediate container 35037b5a7791 step 3/6 : add docker.jar app.jar ---> 06bcfdcff437 removing intermediate container 2e2e5e559ae4 step 4/6 : run bash -c 'touch /app.jar' ---> running in 16441febc271 ---> 1779caa23f77 removing intermediate container 16441febc271 step 5/6 : expose 8080 ---> running in 14dd752ce247 ---> 505044f5cdf8 removing intermediate container 14dd752ce247 step 6/6 : entrypoint java -djava.security.egd=file:/dev/./urandom -jar /app.jar ---> running in 832e4ca95dd2 ---> 1cb7bd139478 removing intermediate container 832e4ca95dd2 successfully built 1cb7bd139478 [root@topcheer docker]# docker images repository tag image id created size boot latest 1cb7bd139478 about a minute ago 677 mb registry.cn-hangzhou.aliyuncs.com/dalianpai/topcheer zipkin 17c2bb09f482 6 days ago 154 mb docker.io/mysql latest c8ee894bd2bd 11 days ago 456 mb elasticsearch latest 874179f19603 4 weeks ago 771 mb docker.io/nacos/nacos-server latest a4229ac5cc19 4 weeks ago 710 mb springbootdemo4docker latest cd13bc7f56a0 5 weeks ago 678 mb docker.io/tomcat latest ee48881b3e82 6 weeks ago 506 mb docker.io/rabbitmq latest a00bc560660a 6 weeks ago 147 mb docker.io/centos latest 67fa590cfc1c 2 months ago 202 mb docker.io/redis latest f7302e4ab3a8 2 months ago 98.2 mb docker.io/rabbitmq 3.7.16-management 3f92e6354d11 3 months ago 177 mb docker.io/elasticsearch 6.8.0 d0b291d7093b 5 months ago 895 mb docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kb docker.io/java 8 d23bdf5b1b1b 2 years ago 643 mb [root@topcheer docker]# docker run -d -p 8080:8080 boot 882ff5209aa2f40972a914b901750a50320faea65100b33e57b9c8a41533ca0b
測試
感謝各位的閱讀,以上就是“Docker怎么部署springboot項目”的內容了,經過本文的學習后,相信大家對Docker怎么部署springboot項目這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。