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

时间:2019-02-22 18:51:41

标签: maven docker docker-maven-plugin

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

  1. 一个integration-test阶段,用于运行一些数据库以进行集成测试。我将使用项目中的一些资源(定义架构的SQL文件)来构建它。
  2. 在测试通过后(也许在package结束时),并带有完整的应用程序。

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

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

$ 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作为基本图像,但这没有任何意义。

1 个答案:

答案 0 :(得分:1)

请尝试将所需映像的完整配置移至执行中,而不要使用全局插件配置。

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