在持續集成過程當中,項目工程通常使用 Maven 編譯打包,後生成鏡像,經過上傳鏡像,可以大大提升上線效率,同時可以快速動態擴容,快速回滾。docker-maven-plugin 插件能夠幫助咱們在Maven工程中,經過簡單的配置,自動生成鏡像並推送到倉庫中。java
docker-maven-plugin 插件默認鏈接本地 Docker 地址爲:localhost:2375,因此咱們須要先設置下環境變量。git
DOCKER_HOST=tcp://<host>:2375
注意:若是沒有設置 DOCKER_HOST
環境變量,能夠命令行顯示指定 DOCKER_HOST
來執行,如我本機指定 DOCKER_HOST:DOCKER_HOST=unix:///var/run/docker.sock mvn clean install docker:build
。docker
構建鏡像可使用一下兩種方式,第一種是將構建信息指定到 POM 中,第二種是使用已存在的 Dockerfile 構建。shell
FROM
, ENTRYPOINT
, CMD
, MAINTAINER
以及 ADD
信息配置在 POM 中,不須要使用 Dockerfile
配置VOLUME
或其餘 Dockerfile
中的命令的時候,須要建立一個 Dockerfile
,並在 POM 中配置 dockerDirectory
來指定路徑便可。<build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> <baseImage>alpine-3.8.0-docker-image:2.0</baseImage> <maintainer>author author@email.com</maintainer> <workdir>/ROOT</workdir> <cmd>["java", "-version"]</cmd> <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint> <!-- 這裏是複製 jar 包到 docker 容器指定目錄配置 --> <resources> <resource> <targetPath>/ROOT</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
pom.xml 文件配置json
<build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> <!-- 指定 Dockerfile 路徑--> <dockerDirectory>${basedir}/docker</dockerDirectory> <!-- 這裏是複製 jar 包到 docker 容器指定目錄配置,也能夠寫到 Docokerfile 中 --> <resources> <resource> <targetPath>/ROOT</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
${basedir}/docker/Dockerfile 文件配置緩存
FROM java MAINTAINER [author] [author@email.com] WORKDIR /ROOT CMD ["java", "-version"] ENTRYPOINT ["java", "-jar", "${project.build.finalName}.jar"]
綁定 Docker 命令到 Maven 各個階段,能夠把 Docker 分爲 build、tag、push,而後分別綁定 Maven 的 package、deploy 等階段,此時,咱們只須要執行 mvn deploy
就能夠完成整個 build、tag、push 操做了,當咱們執行 mvn build
就只完成 build、tag 操做。此外當想跳過某些步驟或者只執行某個步驟時,不須要修改 POM 文件,只須要指定跳過 docker 某個步驟便可。好比當工程已經配置好了自動化模板了,可是此次咱們只須要打鏡像到本地自測,不想執行 push 階段,那麼此時執行要指定參數 -DskipDockerPush
就可跳過 push 操做了。安全
<build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> <!-- 指定 Dockerfile 路徑--> <dockerDirectory>${basedir}/docker</dockerDirectory> <!-- 這裏是複製 jar 包到 docker 容器指定目錄配置,也能夠寫到 Docokerfile 中 --> <resources> <resource> <targetPath>/ROOT</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <execution> <id>tag-image</id> <phase>package</phase> <goals> <goal>tag</goal> </goals> </execution> <execution> <id>push-image</id> <phase>deploy</phase> <goals> <goal>push</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
當咱們執行 mvn package
時,執行 build、tag 操做,當執行 mvn deploy
時,執行 build、tag、push 操做。若是想跳過 docker 某個過程時,只須要:maven
例:咱們想執行 package 時,跳過 tag 過程,那麼就須要 mvn package -DskipDockerTag
。tcp
<servers> <server> <id>docker-registry</id> <username>author-username</username> <password>author-password</password> <configuration> <email>author@mail.com</email> </configuration> </server> </servers>
<plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> ... <serverId>docker-registry</serverId> </configuration> </plugin> </plugins>
docker-maven-plugin 插件還提供了不少很實用的配置參數ui
參數 | 說明 | 默認值 |
---|---|---|
<forceTags>true</forceTags> |
build 時強制覆蓋 tag,配合 imageTags 使用 | false |
<noCache>true</noCache> |
build 時,指定 –no-cache 不使用緩存 | false |
<pullOnBuild>true</pullOnBuild> |
build 時,指定 –pull=true 每次都從新拉取基礎鏡像 | false |
<pushImage>true</pushImage> |
build 完成後 push 鏡像 | false |
<pushImageTag>true</pushImageTag> |
build 完成後,push 指定 tag 的鏡像,配合 imageTags 使用 | false |
<retryPushCount>5</retryPushCount> |
push 鏡像失敗,重試次數 | 5 |
<retryPushTimeout>10</retryPushTimeout> |
push 鏡像失敗,重試時間 | 10s |
<rm>true</rm> |
build 時,指定 –rm=true 即 build 完成後刪除中間容器 | false |
<useGitCommitId>true</useGitCommitId> |
build 時,使用最近的 git commit id 前7位做爲tag,例如:image:82016a5,前提是不配置 newName | false |
[注]: Docker 鏡像名稱需匹配[a-z0-9-_.] 。
docker 刪除容器的時候出現的 bug 解決方案 vi /etc/docker/daemon.json 的參數 "live-restore":false /usr/lib/systemd/system/docker.service 添加了參數 MountFlags=slave