最簡單的spring boot打包docker鏡像的實(shí)現(xiàn)
這個(gè)spring boot項(xiàng)目只在網(wǎng)頁輸出一個(gè)hello world文本,沒有其他復(fù)雜的配置和頁面,屬于入門級,可以放心食用。
本項(xiàng)目通過maven打包,打包和構(gòu)建鏡像的命令為:
mvn clean install package docker:build
spring boot打包docker鏡像步驟如下:
(一)
在pom.xml文件中添加docker配置:
<!--docker maven plugin,在目錄src/main/docker下創(chuàng)建Dockerfile文件,Dockerfile文件用來說明如何構(gòu)建按鏡像--><plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory><!--下面的.jar不要忘記寫,否則會報(bào)“ Exception caught: ADD failed: stat /var/lib/docker/tmp/docker-builder646478477/yang-0.0.1.jar: no such file or directory”錯(cuò)誤--> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration></plugin>
(二)
在src/main/docker文件夾(新建文件夾)下建Dockerfile文件,該文件不要后綴,可以新建一個(gè)txt文件,再把.txt后綴去掉,文件內(nèi)容如下:
From java:8VOLUME /TMPADD yang-0.0.1.jar /yang-0.0.1.jarENTRYPOINT ['java','-Djava.security.egd=file:/dev/./urandom','-jar','/yang.jar']
ADD yang-0.0.1.jar /yang-0.0.1.jar的名字 要和pom的<artifactId>yang</artifactId>
保持名字一樣,不然maven打出來的包,docker找不到。
比如,我這個(gè)項(xiàng)目中的<artifactId>標(biāo)簽內(nèi)容為:
pom.xml文件:
<artifactId>yang</artifactId><version>0.0.1</version><name>yang</name><packaging>jar</packaging>
Dockerfile文件:
ADD yang-0.0.1.jar /yang-0.0.1.jar
ADD yang-0.0.1.jar /yang-0.0.1.jar這一句前面的jar包是本地打包的jar包名稱,后面是復(fù)制到docker后的重命名。
本項(xiàng)目完整的pom.xml文件內(nèi)容如下,各位可以作為參考:
<?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.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springboot</groupId> <artifactId>yang</artifactId> <version>0.0.1</version> <name>yang</name> <packaging>jar</packaging> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <!--<docker.image.prefix>yang-0.0.1</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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--redis--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis --> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-redis</artifactId>--> <!--<version>1.4.7.RELEASE</version>--> <!--</dependency>--> <!--<!– https://mvnrepository.com/artifact/mysql/mysql-connector-java –>--> <!--<dependency>--> <!--<groupId>mysql</groupId>--> <!--<artifactId>mysql-connector-java</artifactId>--> <!--<version>8.0.18</version>--> <!--</dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--docker maven plugin,在目錄src/main/docker下創(chuàng)建Dockerfile文件,Dockerfile文件用來說明如何構(gòu)建按鏡像--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <!--<imageName>${docker.image.prefix}/${project.artifactId}</imageName>--> <imageName>${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>
到此這篇關(guān)于最簡單的spring boot打包docker鏡像的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)spring boot打包docker鏡像內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總3. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送4. python 爬取嗶哩嗶哩up主信息和投稿視頻5. 關(guān)于HTML5的img標(biāo)簽6. Python編寫nmap掃描工具7. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效8. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值9. php5.6不能擴(kuò)展redis.so的解決方法10. python 如何停止一個(gè)死循環(huán)的線程
