继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

使用Maven插件构建与发布Docker镜像

ibeautiful
关注TA
已关注
手记 496
粉丝 108
获赞 529

环境

步骤

1. 编辑POM文件

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <execution>
            <id>tag-latest</id>
            <phase>deploy</phase>
            <goals>
                <goal>build</goal>
                <goal>tag</goal>
                <goal>push</goal>
            </goals>
            <configuration>
                <tag>latest</tag>
            </configuration>
        </execution>
        <execution>
            <id>tag-version</id>
            <phase>deploy</phase>
            <goals>
                <goal>build</goal>
                <goal>tag</goal>
                <goal>push</goal>
            </goals>
            <configuration>
                <tag>${project.version}</tag>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <!--子用户名@企业别名 或 子用户名@主账号UID-->
        <username>镜像仓库用户名</username>
        <!--在容器镜像服务控制台"设置Registry登陆密码"-->
        <password>镜像仓库密码</password>
        <!--registry.cn-hangzhou.aliyuncs.com/namespace/repositoryname-->
        <repository>镜像仓库地址</repository>
        <tag>latest</tag>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration></plugin>

2. 编辑Dockerfile

FROM java:8-jdk-alpine

ARG JAR_FILE
ADD target/${JAR_FILE} app.jar

EXPOSE 8080

ENTRYPOINT ["/usr/bin/java", "-jar", "/app.jar"]

3. 执行构建命令

先查看docker环境变量

$ docker-machine envexport DOCKER_TLS_VERIFY="1"export DOCKER_HOST="tcp://192.168.99.100:2376"export DOCKER_CERT_PATH="C:\Users\shiji\.docker\machine\machines\default"export DOCKER_MACHINE_NAME="default"export COMPOSE_CONVERT_WINDOWS_PATHS="true"

再通过命令行构建

# 不设置环境变量会报错:# Connect to localhost:2375 failed: Connection refusedset DOCKER_HOST=https://192.168.99.100:2376set DOCKER_CERT_PATH=C:\Users\shiji\.docker\machine\machines\default# 生成jar包mvn package -Dmaven.test.skip=true# build imagemvn dockerfile:build# push imagemvn dockerfile:push@tag-version
mvn dockerfile:push@tag-latest



作者:核子飞弹
链接:https://www.jianshu.com/p/c0be287ee44e

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP