Docker使用 Maven 插件構(gòu)建鏡像的方法
通過 Maven 的 Docker 插件可以構(gòu)建 Docker 鏡像
快速入門
在 pom.xml 中添加 Docker 插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>linyuantongxue/docker-demo:0.0.1</imageName> // 指定鏡像名稱,linyuantongxue 是倉(cāng)庫(kù)名稱(對(duì)應(yīng) DockerHub 用戶名),docker-demo 是鏡像名稱(對(duì)應(yīng) DockerHub 倉(cāng)庫(kù)名),0.0.1 是標(biāo)簽名稱(相當(dāng)于版本號(hào))
<baseImage>java</baseImage> // 指定基礎(chǔ)鏡像,等同 FROM 指令
<entryPoint>["java","-jar","app.jar"]</entryPoint> // 等同于 ENTRYPOINT 指令
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory> // 指定要復(fù)制的根目錄,${project.build.directory} 表示 target 目錄
<include>${project.build.finalName}.jar</include> // 指定要復(fù)制的文件,${project.build.finalName}.jar 指打包后的 jar 文件
</resource>
</resources>
</configuration>
</plugin>
執(zhí)行以下命令構(gòu)建 Docker 鏡像
mvn clean package docker:build
執(zhí)行 docker images 查看剛才構(gòu)建的鏡像
讀取 Dockerfile 文件
讀取 Dockerfile 文件就不必指定 baseImage 和 entrypoint
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> // 指定要讀取的 Dockerfile 文件
<imageName>linyuantongxue/docker-demo:0.0.1</imageName> // 指定鏡像名稱,linyuantongxue 是倉(cāng)庫(kù)名稱(對(duì)應(yīng) DockerHub 用戶名),docker-demo 是鏡像名稱(對(duì)應(yīng) DockerHub 倉(cāng)庫(kù)名),0.0.1 是標(biāo)簽名稱(相當(dāng)于版本號(hào))
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory> // 指定要復(fù)制的根目錄,${project.build.directory} 表示 target 目錄
<include>${project.build.finalName}.jar</include> // 指定要復(fù)制的文件,${project.build.finalName}.jar 指打包后的 jar 文件
</resource>
</resources>
</configuration>
</plugin>
將插件綁定在某個(gè) phase 執(zhí)行
很多場(chǎng)景下有這樣的需求,比如執(zhí)行 mvn clean package 時(shí)插件就自動(dòng)構(gòu)建 Docker 鏡像,要實(shí)現(xiàn)這點(diǎn)只需要將插件的 goal 綁定在某個(gè) phase 即可
maven 命令格式是:mvn phase:goal,phase 綁定了目標(biāo)的構(gòu)建生命周期階段,goal 配置的執(zhí)行目標(biāo)
只需添加如下配置:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
// 在 maven 生命周期 package 中執(zhí)行 build 構(gòu)建目標(biāo)
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
// $$$$$$$$$$$$$$$$華麗的分割線$$$$$$$$$$$$$$$$
<configuration>
<imageName>linyuantongxue/docker-demo:0.0.1</imageName>
<baseImage>java</baseImage>
<entryPoint>["java","-jar","app.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
推送鏡像
使用 Maven 插件也可以推送鏡像到 Docker Hub
修改 Maven 全局配置信息文件 settings.xml,配置 Docker Hub 用戶信息
<servers>
<server>
<id>docker-hub</id>
# DockerHub 該網(wǎng)站的用戶名必須全部為小寫才正確
<username>linyuantongxue</username>
<password>765371578Ly</password>
<configuration>
<email>765371578@qq.com</email>
</configuration>
</server>
</servers>
修改 pom 文件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>linyuantongxue/docker-demo:0.0.1</imageName>
<baseImage>java</baseImage>
<entryPoint>["java","-jar","app.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!--與配置文件 setting.xml 中的 server.id 一致,用于推送鏡像-->
<serverId>docker-hub</serverId>
</configuration>
</plugin>
執(zhí)行以下命令,添加 pushImage 標(biāo)識(shí),表示推送鏡像
mvn clean package docker:build -DpushImage
上面例子中通過 imageName 指定鏡像名稱和標(biāo)簽,也可以借助 imageTags 元素更為靈活的指定鏡像名稱和標(biāo)簽,這樣就可以為同一個(gè)鏡像指定兩個(gè)標(biāo)簽
<configuration>
<imageName>linyuantongxue/docker-demo</imageName>
<imageTags>
<imageTag>0.0.1</imageTag>
<imageTag>latest</imageTag>
</imageTags>
</configuration>
也可在構(gòu)建命令時(shí)使用 dockerImageTags 參數(shù)指定標(biāo)簽名稱
mvn clean package:build -DpushImageTags -DdockerImageTags=latest -DdockerImageTags=another-tag
若需要重復(fù)構(gòu)建相同標(biāo)簽名稱的鏡像,可將 forceTags 設(shè)置為 true
<configuration> // ....... <forceTags>true</forceTags> </configuration>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目引入第三方sdk?jar包的解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目引入第三方sdk?jar包,個(gè)人感覺比較好的解決方案是將 jar上傳到本地的maven倉(cāng)庫(kù),然后通過pom依賴,引入第三方j(luò)ar包,需要的朋友可以參考下2022-05-05
一文告訴你為什么要重寫hashCode()方法和equals()方法
本篇文章帶大家了解一下為什么重寫hashCode()方法和equals()方法,文中有非常詳細(xì)的說明以及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05

