通過 Maven 的 Docker 插件可以構建 Docker 鏡像
快速入門
在 pom.xml 中添加 Docker 插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< 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 鏡像
1
|
mvn clean package docker:build |
執行 docker images 查看剛才構建的鏡像
讀取 Dockerfile 文件
讀取 Dockerfile 文件就不必指定 baseImage 和 entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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 配置的執行目標
只需添加如下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
< 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 用戶信息
1
2
3
4
5
6
7
8
9
10
11
|
< servers > < server > < id >docker-hub</ id > # DockerHub 該網站的用戶名必須全部為小寫才正確 < username >linyuantongxue</ username > < password >765371578Ly</ password > < configuration > </ configuration > </ server > </ servers > |
修改 pom 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< 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 標識,表示推送鏡像
1
|
mvn clean package docker:build -DpushImage |
上面例子中通過 imageName 指定鏡像名稱和標簽,也可以借助 imageTags 元素更為靈活的指定鏡像名稱和標簽,這樣就可以為同一個鏡像指定兩個標簽
1
2
3
4
5
6
7
|
< 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
1
2
3
4
|
< configuration > // ....... < forceTags >true</ forceTags > </ configuration > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/c9d771a193b4