How to restart Docker container before each test execution with Maven?

1.8k views Asked by At

I would like to start a fresh postgres docker container before each single test execution, and NOT just once before running all integration tests. The configuration I currently have is like most Google results suggest:

     <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.33.0</version>
                <configuration>
                    <imagePullPolicy>IfNotPresent</imagePullPolicy>
                    <images>
                        <image>
                            <alias>it-database</alias>
                            <name>postgres:11.3</name>
                            <run>
                                <namingStrategy>alias</namingStrategy>
                                <ports>
                                    <port>5555:5432</port>
                                </ports>
                                <wait>
                                    <log>(?s)database system is ready to accept connections.*database system is ready to accept connections
                                    </log>
                                    <time>20000</time>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
                <executions>
                    <execution>
                        <id>docker:start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>docker:stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
1

There are 1 answers

5
khmarbaise On

I strongly recommend to take a deep look into Testcontainers cause testcontainers can exactly do what you like within your integration test.