如何在单独的阶段中从Maven构建2个Docker图像



我在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>

相关内容

  • 没有找到相关文章

最新更新