如何在不同的阶段从 Maven 构建 2 docker 个图像?

How do I build 2 docker images from maven, in separate phases?

我在 Maven 项目中使用 io.fabric8:docker-maven-plugin:0.28.0,我需要在不同的阶段构建 2 个不同的 Docker 图像:

  1. 一个用于 integration-test 阶段,运行 一些数据库用于集成测试。我会使用项目中的一些资源(SQL 定义模式的文件)来构建它。
  2. 最后一个,在测试通过后(可能在 package 期间),以及完整的应用程序。

我的问题是只构建了第一个图像。

我试图在 <execution> 元素的 <configuration> 中引用图像别名,但它似乎不起作用。仅考虑第一张图片:

$ mvn install

...
[INFO] --- docker-maven-plugin:0.28.0:build (start) @ dmp-example ---
[INFO] Building tar: /home/cosmin/workspaces/photon/test/dmp-example/target/docker/example/db-test/latest/tmp/docker-build.tar
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Created docker-build.tar in 28 milliseconds
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Built image sha256:d8233
[INFO] 
[INFO] --- docker-maven-plugin:0.28.0:start (start) @ dmp-example ---
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Start container 47883d799641
[INFO] 
[INFO] --- docker-maven-plugin:0.28.0:stop (stop) @ dmp-example ---
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Stop and removed container 47883d799641 after 0 ms
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ dmp-example ---
[INFO] Installing /home/cosmin/workspaces/photon/test/dmp-example/target/dmp-example-0.0.1-SNAPSHOT.jar to /home/cosmin/.m2/repository/com/example/dmp-example/0.0.1-SNAPSHOT/dmp-example-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/cosmin/workspaces/photon/test/dmp-example/pom.xml to /home/cosmin/.m2/repository/com/example/dmp-example/0.0.1-SNAPSHOT/dmp-example-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] --- docker-maven-plugin:0.28.0:build (build) @ dmp-example ---
[INFO] Building tar: /home/cosmin/workspaces/photon/test/dmp-example/target/docker/example/db-test/latest/tmp/docker-build.tar
[INFO] DOCKER> [example/db-test:latest] "example-packaged": Created docker-build.tar in 6 milliseconds
[INFO] DOCKER> [example/db-test:latest] "example-packaged": Built image sha256:d8233
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

第二个镜像没有创建,应该构建第二个时也使用第一个的ID:

$ docker images "example/*"
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
example/db-test     latest              d8233ab899d4        7 days ago          1.2MB

还有其他方法吗?

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>dmp-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.28.0</version>
                <configuration>
                    <images>
                        <image>
                            <alias>example-db-test</alias>
                            <name>example/db-test:latest</name>
                            <build>
                                <from>busybox</from>
                            </build>
                        </image>
                        <image>
                            <alias>example-packaged</alias>
                            <name>example/packaged:latest</name>
                            <build>
                                <from>alpine</from>
                            </build>
                        </image>
                    </images>
                </configuration>
                <executions>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <images>
                                <image>
                                    <alias>example-db-test</alias>
                                </image>
                            </images>
                        </configuration>
                        <goals>
                            <goal>build</goal>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <configuration>
                            <images>
                                <image>
                                    <alias>example-db-test</alias>
                                </image>
                            </images>
                        </configuration>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>build</id>
                        <phase>package</phase>
                        <configuration>
                            <images>
                                <image>
                                    <alias>example-packaged</alias>
                                </image>
                            </images>
                        </configuration>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

为了简化示例,我使用 busyboxalpine 作为基础图像,但这没有任何关系。

尝试将您想要的图像的完整配置移动到执行中,而不是使用全局插件配置。

<execution>
    <id>build</id>
    <phase>package</phase>
    <configuration>
        <images>
            <image>
                <alias>example-packaged</alias>
                <name>example/packaged:latest</name>
                <build>
                    <from>alpine</from>
                </build>
            </image>
        </images>
    </configuration>
    <goals>
        <goal>build</goal>
    </goals>
</execution>