dockerfile-maven-plugin 使用
這是一個簡單的Springboot示例,用于演示 dockerfile-maven-plugin 把Maven項目生成Docker Image并Push到私有Registry。
com.spotify有兩個插件,網(wǎng)上的資料大都基于老的docker-maven-plugin,在其主頁上已經(jīng)說明:
我們建議新項目使用dockerfile-maven。docker-maven-plugin將不再具有新功能或接受新功能的PR。但是,我們將繼續(xù)提供BUG修復(fù)。
這個插件是Spotify用于從Java服務(wù)構(gòu)建Docker Image的初始Maven插件。它最初創(chuàng)建于2014年,當(dāng)時我們第一次開始嘗試使用Docker。此插件能夠根據(jù)pom.xml文件中的配置為您生成Dockerfile,用于FROM Image,使用ADD / COPY添加的資源等。隨著時間的推移,我們已經(jīng)意識到從Java項目構(gòu)建Docker Image的最簡單方法是讓開發(fā)人員編寫Dockerfile。這個插件圍繞生成Dockerfiles,將項目目錄復(fù)制到“staging”目錄以用作Docker構(gòu)建上下文等的行為最終導(dǎo)致了很多不必要的混淆,因為我們的用戶引入了額外的抽象和需求用于配置Docker提供的功能。這導(dǎo)致了用于構(gòu)建docker Image的第二個Maven插件,dockerfile-maven,我們認為這提供了一個更簡單的使用Maven生成Docker Image的方法。
雖然說是更簡單,但是實際使用中因為文檔比較簡略,還是有坑的,現(xiàn)記錄如下。
- 報錯:
Could not acquire image ID or digest following build
在1.3.X版本會出現(xiàn),更新到1.4.X后修復(fù)。 - 報錯:
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
引入插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
dockerfile-maven-plugin Maven Goals:
Maven Goals
Goals available for this plugin:
| Goal | Description | Default Phase |
|---|---|---|
dockerfile:build |
Builds a Docker image from a Dockerfile. | package |
dockerfile:tag |
Tags a Docker image. | package |
dockerfile:push |
Pushes a Docker image to a repository. | deploy |
Skip Docker Goals Bound to Maven Phases
You can pass options to maven to disable the docker goals.
| Maven Option | What Does it Do? | Default Value |
|---|---|---|
dockerfile.skip |
Disables the entire dockerfile plugin; all goals become no-ops. | false |
dockerfile.build.skip |
Disables the build goal; it becomes a no-op. | false |
dockerfile.tag.skip |
Disables the tag goal; it becomes a no-op. | false |
dockerfile.push.skip |
Disables the push goal; it becomes a no-op. | false |
如果package時不想打包Image可以這樣:
mvn clean package -Ddockerfile.skip
Configuration
Build Phase
| Maven Option | What Does it Do? | Required | Default Value |
|---|---|---|---|
dockerfile.contextDirectory |
Directory containing the Dockerfile to build. | yes | none |
dockerfile.repository |
The repository to name the built image | no | none |
dockerfile.tag |
The tag to apply when building the Dockerfile, which is appended to the repository. | no | latest |
dockerfile.build.pullNewerImage |
Updates base images automatically. | no | true |
dockerfile.build.noCache |
Do not use cache when building the image. | no | false |
dockerfile.build.cacheFrom |
Docker image used as cache-from. Pulled in advance if not exist locally or pullNewerImage is false
|
no | none |
dockerfile.buildArgs |
Custom build arguments. | no | none |
dockerfile.build.squash |
Squash newly built layers into a single new layer (experimental API 1.25+). | no | false |
示例代碼
POM
<project>
<properties>
<java.version>1.8</java.version>
<!--docker私服地址-->
<docker.repostory>192.168.87.110:5000</docker.repostory>
</properties>
<!--略-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<!--如果package時不想用docker打包,就注釋掉這個goal-->
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.repostory}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<!--提供參數(shù)向Dockerfile傳遞-->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Dockerfile文件
FROM openjdk:8u191-jre-alpine3.9
ENTRYPOINT ["/usr/bin/java", "-jar", "/app.jar"]
ARG JAR_FILE
ADD ${JAR_FILE} /app.jar
EXPOSE 8080
打包并推送
mvn deploy
請確認能登錄registry
cat ~/.docker/config.json
{
"auths": {
"192.168.87.110:5000": {
"auth": "YWRtaW46JKDtaW4xMjM="
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.0 (linux)"
}
}
如果沒有,docker login 192.168.87.110:5000
手工這樣是不方便的,可以配置處理,請參閱
Authentication
可以在maven settings.xml或pom.xml中配置,如果要對settings.xml中的密碼進行加密,請參閱Password Encryption (從Version 1.4.3支持)