最簡(jiǎn)單的spring boot打包docker鏡像的實(shí)現(xiàn)
這個(gè)spring boot項(xiàng)目只在網(wǎng)頁(yè)輸出一個(gè)hello world文本,沒(méi)有其他復(fù)雜的配置和頁(yè)面,屬于入門級(jí),可以放心食用。
本項(xiàng)目通過(guò)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文件用來(lái)說(shuō)明如何構(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不要忘記寫,否則會(huì)報(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打出來(lái)的包,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文件用來(lái)說(shuō)明如何構(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)于最簡(jiǎn)單的spring boot打包docker鏡像的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)spring boot打包docker鏡像內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼2. XML入門的常見(jiàn)問(wèn)題(四)3. XML基本概念XPath、XSLT與XQuery函數(shù)介紹4. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)5. 不要在HTML中濫用div6. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題7. vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解8. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決9. XML入門的常見(jiàn)問(wèn)題(三)10. WML的簡(jiǎn)單例子及編輯、測(cè)試方法第1/2頁(yè)
