手记

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

环境

步骤

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

0人推荐
随时随地看视频
慕课网APP