使用Maven插件构建Docker镜像并上传

工具

插件名称官方地址
docker-maven-pluginhttps://github.com/spotify/docker-maven-plugin

在pom.xml中添加下面这段(Dockerfile构建)

<!-- Docker镜像构建 mvn docker:build https://github.com/spotify/docker-maven-plugin  -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.1.1</version>
    <configuration>
        <!-- Dockerfile文件位置 -->
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <!-- 镜像仓库:版本 -->
        <imageName>registry.cn-shanghai.aliyuncs.com/ycmm/${project.artifactId}:${project.version}</imageName>
        <!-- 强制覆盖 -->
        <forceTags>true</forceTags>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>
        <!-- 资源 -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
        <serverId>docker-ali</serverId>
        <registryUrl>registry.cn-shanghai.aliyuncs.com</registryUrl>
    </configuration>
</plugin>

构建命令

mvn clean package docker:build

将插件绑定在某个phase执行

  • mvn install 时能够docker:build
  • mvn deploy 时能够docker:push

我们只需要将插件的goal 绑定在某个phase即可。

所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那么,package 和docker 都是phase,build 则是goal 。

修改POM文件
<!-- Docker镜像构建 mvn docker:build https://github.com/spotify/docker-maven-plugin  -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.1.1</version>
    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
        <execution>
            <id>push-image</id>
            <phase>deploy</phase>
            <goals>
                <goal>push</goal>
            </goals>
            <configuration>
                <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
            </configuration>
        </execution>   
    </executions>
    <configuration>
        <!-- Dockerfile文件位置 -->
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <!-- 镜像仓库:版本 -->
        <imageName>registry.cn-shanghai.aliyuncs.com/ycmm/${project.artifactId}:${project.version}</imageName>
        <!-- 强制覆盖 -->
        <!-- <forceTags>true</forceTags>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags> -->
        <!-- 资源 -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
        <serverId>docker-ali</serverId>
        <registryUrl>registry.cn-shanghai.aliyuncs.com</registryUrl>
    </configuration>
</plugin>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值