fabric8io / docker-maven-plugin

Maven plugin for running and creating Docker images
https://dmp.fabric8.io
Apache License 2.0
1.87k stars 640 forks source link

Plugin fails to start docker comtainer when multiple maven profiles are executed #667

Open bhathiya opened 7 years ago

bhathiya commented 7 years ago

In my maven project, I have multiple maven profiles. In each profile, I have docker-maven-plugin and maven-failsafe-plugin. I have profiles for each database type (i.e. MySQL, Postgres etc.). What I'm trying to do is that run my integration tests on docker for each database type.

       <profile>
            <id>local-mysql</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>0.18.1</version>
                        <executions>
                            <execution>
                                <id>start-docker-for-mysql</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>stop-docker-for-mysql</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                    <goal>remove</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <images>
                                <image>
                                    <alias>apim-mysql</alias>
                                    <name>mysql:5.7</name>
                                    <run>
                                        <namingStrategy>alias</namingStrategy>
                                        <env>
                                            <MYSQL_DATABASE>testamdb</MYSQL_DATABASE>
                                            <MYSQL_ROOT_PASSWORD>root</MYSQL_ROOT_PASSWORD>
                                        </env>
                                        <wait>
                                            <tcp>
                                                <ports>
                                                    <port>3306</port>
                                                </ports>
                                            </tcp>
                                            <time>60000</time>
                                        </wait>
                                    </run>
                                </image>
                            </images>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <executions>
                            <execution>
                                <id>integration-test-for-mysql</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>verify-for-mysql</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>local-postgres</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>0.18.1</version>
                        <executions>
                            <execution>
                                <id>start-docker-for-postgres</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>stop-docker-for-postgres</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                    <goal>remove</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <images>
                                <image>
                                    <alias>apim-postgres</alias>
                                    <name>postgres:9.6-alpine</name>
                                    <run>
                                        <namingStrategy>alias</namingStrategy>
                                        <env>
                                            <POSTGRES_PASSWORD>root</POSTGRES_PASSWORD>
                                            <POSTGRES_USER>root</POSTGRES_USER>
                                            <POSTGRES_DB>testamdb</POSTGRES_DB>
                                        </env>
                                        <wait>
                                            <tcp>
                                                <ports>
                                                    <port>5432</port>
                                                </ports>
                                            </tcp>
                                            <time>60000</time>
                                        </wait>
                                    </run>
                                </image>
                            </images>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <executions>
                            <execution>
                                <id>integration-test-for-postgres</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>verify-for-postgres</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

However, when I run this command,

mvn clean install -P local-mysql,local-postgres

it fails with below error.

[INFO] --- docker-maven-plugin:0.18.1:start (start-docker-for-mysql) @ org.wso2.carbon.apimgt.core ---
[INFO] DOCKER> [postgres:9.6-alpine] "apim-postgres": Start container afdf04e8a129
[INFO] DOCKER> [postgres:9.6-alpine] "apim-postgres": Waiting for ports [5432] directly on container with IP (172.17.0.2).
[INFO] DOCKER> [postgres:9.6-alpine] "apim-postgres": Waited on tcp port '[/172.17.0.2:5432]' 4006 ms
[INFO] 
[INFO] --- docker-maven-plugin:0.18.1:start (start-docker-for-postgres) @ org.wso2.carbon.apimgt.core ---
[ERROR] DOCKER> Error occurred during container startup, shutting down...
[INFO] DOCKER> [postgres:9.6-alpine] "apim-postgres": Stop and removed container afdf04e8a129 after 0 ms
[ERROR] DOCKER> I/O Error
[INFO] ------------------------------------------------------------------------

If you look clearly, it starts Postgres container at start-docker-for-MySQL execution, which is wrong. And it fails when it tries to start Postgres container again in start-docker-for-Postgres execution which makes sense, due to the previous issue.

I believe this is a bug. If not, please let me know what I'm doing wrong.

hwellmann commented 7 years ago

Maven plugin configuration is additive when you activate multiple profiles simultaneously.

So d-m-p will try to start all defined images in every start execution.

To avoid that, just try including a suitable <filter> tag in each execution configuration, to explicitly list the images to be started or stopped.

bhathiya commented 7 years ago

Does docker-maven-plugin support filtering? Could you please give me a sample?

hwellmann commented 7 years ago
                        <execution>
                            <id>default-start</id>
                            <goals>
                                <goal>start</goal>
                            </goals>
                            <configuration>
                                <filter>mysql</filter>
                            </configuration>
                        </execution>
bhathiya commented 7 years ago

I read about it here, but couldn't find a complete example anywhere. I tried many different things, but none of them worked.

In this case, how can I specify my image list? Appreciate some guidance.

seckinsq commented 5 years ago

@bhathiya did you make any progress on running multiple profiles (2 years ago) ?

bhathiya commented 5 years ago

Nope. Configured Jenkins to run each profile separately.

bhathiya commented 5 years ago

Forgot this. I created a single profile to wrap others. See https://github.com/wso2/carbon-apimgt/blob/master/components/apimgt/org.wso2.carbon.apimgt.core/pom.xml#L316

seckinsq commented 5 years ago

thanks @bhathiya for sharing. mine was a bit simpler, so I gave up and run all images. And disable/enable tests that use that additional image. The image that I wanted to add with certain profile was a small one. If I need more then I will certainly use your solution. Thanks again.