Docker使用 Maven 外掛程式構建鏡像的方法,dockermaven
通過 Maven 的 Docker 外掛程式可以構建 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 是倉庫名稱(對應 DockerHub 使用者名稱),docker-demo 是鏡像名稱(對應 DockerHub 倉庫名),0.0.1 是標籤名稱(相當於版本號碼) <baseImage>java</baseImage> // 指定基礎鏡像,等同 FROM 指令 <entryPoint>["java","-jar","app.jar"]</entryPoint> // 等同於 ENTRYPOINT 指令 <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> // 指定要複製的根目錄,${project.build.directory} 表示 target 目錄 <include>${project.build.finalName}.jar</include> // 指定要複製的檔案,${project.build.finalName}.jar 指打包後的 jar 檔案 </resource> </resources> </configuration></plugin>
執行以下命令構建 Docker 鏡像
mvn clean package docker:build
執行 docker images 查看剛才構建的鏡像
讀取 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 是倉庫名稱(對應 DockerHub 使用者名稱),docker-demo 是鏡像名稱(對應 DockerHub 倉庫名),0.0.1 是標籤名稱(相當於版本號碼) <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> // 指定要複製的根目錄,${project.build.directory} 表示 target 目錄 <include>${project.build.finalName}.jar</include> // 指定要複製的檔案,${project.build.finalName}.jar 指打包後的 jar 檔案 </resource> </resources> </configuration></plugin>
將外掛程式綁定在某個 phase 執行
很多情境下有這樣的需求,比如執行 mvn clean package 時外掛程式就自動構建 Docker 鏡像,要實現這點只需要將外掛程式的 goal 綁定在某個 phase 即可
maven 命令格式是:mvn phase:goal,phase 綁定了目標的構建生命週期階段,goal 配置的執行目標
只需添加如下配置:
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> // 在 maven 生命週期 package 中執行 build 構建目標 <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 該網站的使用者名稱必須全部為小寫才正確 <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>
執行以下命令,添加 pushImage 標識,表示推送鏡像
mvn clean package docker:build -DpushImage
上面例子中通過 imageName 指定鏡像名稱和標籤,也可以藉助 imageTags 元素更為靈活的指定鏡像名稱和標籤,這樣就可以為同一個鏡像指定兩個標籤
<configuration> <imageName>linyuantongxue/docker-demo</imageName> <imageTags> <imageTag>0.0.1</imageTag> <imageTag>latest</imageTag> </imageTags></configuration>
也可在構建命令時使用 dockerImageTags 參數指定標籤名稱
複製代碼 代碼如下:
mvn clean package:build -DpushImageTags -DdockerImageTags=latest -DdockerImageTags=another-tag
若需要重複構建相同標籤名稱的鏡像,可將 forceTags 設定為 true
<configuration> // ....... <forceTags>true</forceTags></configuration>
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。