1

I use io.fabric8:docker-maven-plugin:0.28.0 in a Maven project and I need to build 2 different Docker images, in different phases:

  1. One for the integration-test phase, to run some database for the integration tests. I would build it with some resources from the project (SQL files defining the schema).
  2. One at the end, after the tests have passed (maybe during package), with the complete application.

My problem is that only the first image is built.

I tried to reference the image aliases in the <configuration> of the <execution> elements, but it doesn't seem to work. Only the first image is considered:

$ 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] ------------------------------------------------------------------------
...

The second image is not created, and the ID of the first one is also used when the second one should be built:

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

Would there be another way of doing it?

This is my 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>

For keeping the example simple, I used busybox and alpine as the base images, but that has no relevance.

Cos64
  • 1,617
  • 2
  • 19
  • 30
  • The last execution of the docker-maven-plugin is missing the phase; is that just a typo? If not, could be the issue. – user944849 Feb 22 '19 at 19:52
  • I just added the `package` phase (and updated the question), but it doesn't make any difference. I think the `package` phase is the default. Also, you can see in the console output that `example-packaged` is mentioned, but with the same ID/hash (`d8233`) as `example-db-test`. – Cos64 Feb 23 '19 at 06:10

1 Answers1

3

Try moving the full configuration for the image you want into the execution, instead of using global plugin config.

<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>
user944849
  • 14,524
  • 2
  • 61
  • 83