使用dockerfile-maven-plugin打包maven項(xiàng)目為docker image

dockerfile-maven-plugin 使用

這是一個(gè)簡(jiǎn)單的Springboot示例,用于演示 dockerfile-maven-plugin 把Maven項(xiàng)目生成Docker Image并Push到私有Registry。

com.spotify有兩個(gè)插件,網(wǎng)上的資料大都基于老的docker-maven-plugin,在其主頁(yè)上已經(jīng)說明:

我們建議新項(xiàng)目使用dockerfile-maven。docker-maven-plugin將不再具有新功能或接受新功能的PR。但是,我們將繼續(xù)提供BUG修復(fù)。
這個(gè)插件是Spotify用于從Java服務(wù)構(gòu)建Docker Image的初始Maven插件。它最初創(chuàng)建于2014年,當(dāng)時(shí)我們第一次開始嘗試使用Docker。此插件能夠根據(jù)pom.xml文件中的配置為您生成Dockerfile,用于FROM Image,使用ADD / COPY添加的資源等。隨著時(shí)間的推移,我們已經(jīng)意識(shí)到從Java項(xiàng)目構(gòu)建Docker Image的最簡(jiǎn)單方法是讓開發(fā)人員編寫Dockerfile。這個(gè)插件圍繞生成Dockerfiles,將項(xiàng)目目錄復(fù)制到“staging”目錄以用作Docker構(gòu)建上下文等的行為最終導(dǎo)致了很多不必要的混淆,因?yàn)槲覀兊挠脩粢肓祟~外的抽象和需求用于配置Docker提供的功能。這導(dǎo)致了用于構(gòu)建docker Image的第二個(gè)Maven插件,dockerfile-maven,我們認(rèn)為這提供了一個(gè)更簡(jiǎn)單的使用Maven生成Docker Image的方法。

雖然說是更簡(jiǎn)單,但是實(shí)際使用中因?yàn)槲臋n比較簡(jiǎn)略,還是有坑的,現(xiàn)記錄如下。

  1. 報(bào)錯(cuò):
    Could not acquire image ID or digest following build
    在1.3.X版本會(huì)出現(xiàn),更新到1.4.X后修復(fù)。
  2. 報(bào)錯(cuò):
    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時(shí)不想打包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時(shí)不想用docker打包,就注釋掉這個(gè)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

請(qǐng)確認(rèn)能登錄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
手工這樣是不方便的,可以配置處理,請(qǐng)參閱
Authentication
可以在maven settings.xmlpom.xml中配置,如果要對(duì)settings.xml中的密碼進(jìn)行加密,請(qǐng)參閱Password Encryption (從Version 1.4.3支持)

Demo源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容