描述

在构建时一同构建docker镜像及推送

POM定义

在pom的description节点中引入properties定义推送的镜像仓库

    <properties>
        <java.version>1.8</java.version>
        <docker.repostory>registry.cn-hangzhou.aliyuncs.com/bohai_repo</docker.repostory>
    </properties>

build节点中引入插件:dockerfile-maven-plugin

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>${docker.repostory}/${project.artifactId}</repository>
                    <!--  镜像tag取用project.version -->
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <!--  在target目录中查找project.build.finalName节点下的jar包 -->
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

新建一个dockerfile

FROM openjdk:8u191-jre-alpine3.9
ENTRYPOINT ["/usr/bin/java", "-jar", "/app.jar"]
# 在pom中定义的 标签
ARG JAR_FILE
ADD ${JAR_FILE} /app.jar
EXPOSE 8080

构建

# 只构建
$ mvn install dockerfile:build

# 构建+ 推送
$ mvn install dockerfile:push

[INFO] --- dockerfile:1.4.10:push (default-cli) @ demo ---
[INFO] The push refers to repository [registry.cn-hangzhou.aliyuncs.com/bohai_repo/demo]
[INFO] Image 72a6d2e10f94: Preparing
[INFO] Image 4222fe8d2ce7: Preparing
[INFO] Image 9afc0e59c268: Preparing
[INFO] Image b83703e07573: Preparing
[INFO] Image 4222fe8d2ce7: Layer already exists
[INFO] Image 9afc0e59c268: Layer already exists
[INFO] Image b83703e07573: Layer already exists
[INFO] Image 72a6d2e10f94: Pushing
[INFO] Image 72a6d2e10f94: Pushed
[INFO] 1.1.0-SNAPSHOT: digest: sha256:091d1dba02edc1754040c34d716001c5c4f30e5059fe387df6572cc1722117d1 size: 1159

其他

在apple silicon的mac中构建略微比较麻烦,需要我们开放docker的api接口

$ brew install socat
$ nohup socat TCP-LISTEN:2375,range=127.0.0.1/32,reuseaddr,fork UNIX-CLIENT:/var/run/docker.sock &> /dev/null &
$ export DOCKER_HOST=tcp://127.0.0.1:2375
Last modification:October 16, 2023
如果觉得我的文章对你有用,请随意赞赏