How to prevent running a docker image each time in docker maven plugin?

1k Views Asked by At

My goal is to start an MSSQL Docker container while building my Spring Boot application with docker-maven-plugin only once, so on future running of 'mvn clean install' i want my build to skip trying to start docker container which is running already, as it reports an error that my DB container already started and my build breaks. Is there any configuration in pom.xml that could be done so I can achieve starting my DB container only if it has not been started already? Maybe some type of check to skip starting DB container if it's already running? My current configuration in pom.xml:

<plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>prepare-it-database</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <images>
                            <image>
                                <name>mcr.microsoft.com/mssql/server</name>
                                <alias>sa</alias>
                                <run>
                                    <env>
                                        <ACCEPT_EULA>Y</ACCEPT_EULA>
                                        <SA_PASSWORD>password</SA_PASSWORD>
                                    </env>
                                    <ports>
                                        <port>1433:1433</port>
                                    </ports>
                                    <wait>
                                        <log>SQL Server is now ready for client connections</log>
                                        <time>20000</time>
                                    </wait>
                                </run>
                            </image>
                        </images>
                    </configuration>
                </execution>
        </plugin>
1

There are 1 best solutions below

0
On

maven is stateless between restarts and know nothing about already reserved resources, in such case may be it would be better to clean resources in the end of build (for instance with mvn-finisher plug-in) and restart docker on next start