最簡單的spring boot打包docker鏡像的實現
這個spring boot項目只在網頁輸出一個hello world文本,沒有其他復雜的配置和頁面,屬于入門級,可以放心食用。
本項目通過maven打包,打包和構建鏡像的命令為:
mvn clean install package docker:build
spring boot打包docker鏡像步驟如下:
(一)
在pom.xml文件中添加docker配置:
<!--docker maven plugin,在目錄src/main/docker下創建Dockerfile文件,Dockerfile文件用來說明如何構建按鏡像--><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不要忘記寫,否則會報“ Exception caught: ADD failed: stat /var/lib/docker/tmp/docker-builder646478477/yang-0.0.1.jar: no such file or directory”錯誤--> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration></plugin>
(二)
在src/main/docker文件夾(新建文件夾)下建Dockerfile文件,該文件不要后綴,可以新建一個txt文件,再把.txt后綴去掉,文件內容如下:
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找不到。
比如,我這個項目中的<artifactId>標簽內容為:
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包名稱,后面是復制到docker后的重命名。
本項目完整的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.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下創建Dockerfile文件,Dockerfile文件用來說明如何構建按鏡像--> <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>
到此這篇關于最簡單的spring boot打包docker鏡像的實現的文章就介紹到這了,更多相關spring boot打包docker鏡像內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: