【Docker入门学习】(六) 使用docker-maven-plugin插件构建docker镜像(已过时)

可以参考博客:https://blog.csdn.net/aixiaoyang168/article/details/77453974

docker-maven-plugin官网推荐在新项目中使用dockerfile-maven来构建镜像。

docker-maven-plugin的Github地址:https://github.com/spotify/docker-maven-plugin

dockerfile-maven的Github地址:https://github.com/spotify/dockerfile-maven

 

一、 使用docker-maven-plugin构建镜像

docker-maven-plugin有两种使用方式,一种是使用Dockerfile文件,一种是不使用Dockerfile文件。

1.在 POM中指定构建信息(不使用Dockerfile文件)

在pom.xml中引入该插件

<!-- docker-maven-plugin插件(不带Dockerfile文件) -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    <configuration>
        <!--用于指定镜像名称-->
        <imageName>${project.name}:${project.version}</imageName>
        <!--用于指定基础镜像,相当于Dockerfile中的FROM指令-->
        <baseImage>java</baseImage>
        <!--相当于Dockerfile的ENTRYPOINT指令-->
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!--是否跳过docker build-->
        <skipDockerBuild>true</skipDockerBuild>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <!--用于指定需要复制的根目录,${project.build.directory}表示target目录-->
                <directory>${project.build.directory}</directory>
                <!--用于指定需要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。-->
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

默认情况下,该插件通过访问localhost:2375来连接本地docker,可以通过设置DOCKER_HOST 环境变量来连接docker.

DOCKER_HOST=tcp://<host>:2375

2.使用Dockerfile文件

如果使用Dockerfile文件,必须指定dockerDirectory元素,那么 baseImage, maintainer, cmd and entryPoint这些元素也会被忽略。dockerDirectory元素所指定的内容将被复制到${project.build.directory}/docker下,resources元素则会复制除此之外的其它文件,例如项目jar文件。

<!--docker-maven-plugin插件(带Dockerfile文件)-->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    <configuration>
        <imageName>${project.name}:${project.version}</imageName>
        <!--Dockerfile文件位置-->
        <dockerDirectory>docker</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

 

二、使用

创建镜像

mvn clean package docker:build

推送镜像到Registry

mvn clean package docker:build -DpushImage

推送指定tag的镜像到Registry

mvn clean package docker:build -DpushImageTag

三、绑定Docker 命令到 Maven 各个阶段

 

posted @ 2018-11-26 17:53  静水楼台/Java部落阁  阅读(9261)  评论(2编辑  收藏  举报