I'm starting postgres container with maven docker plugin and then use the DB to generate some artefacts in later steps.
<plugin>
…
<artifactId>docker-maven-plugin</artifactId>
…
<configuration>
<images>
<image>
...
<name>postgres:11</name>
...
</image>
</images>
</configuration>
<executions>
<execution>
<id>start-postgres-container</id>
<phase>generate-sources</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-postgres-container</id>
<phase>process-sources</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
The issue I have is that when there is some error with any operations between start
and stop
above, then maven leaves the running container and consecutive build attempts will fail so one has to put down the leftover container manually first.
Is there a way/plugin in maven to specify some finally
action/phase? So that in case of failure in build scripts one would still be able to release some resources which might've been already reserved?
The goal could be achieved applying similar solution to what testcontainers are doing using Ryuk: https://github.com/testcontainers/moby-ryuk
The image to reap should be labeled, e.g. as:
<autoRemove>true</autoRemove>
<labels>
<killme>true</killme>
</labels>
<wait>
<time>3000</time>
</wait>
The above delay is to give some time for Ryuk setup. As this one must be started in parallel...
<image>
<alias>ryuk-summoned</alias>
<name>testcontainers/ryuk:0.3.0</name>
<run>
<ports>
<port>ryuk.port:8080</port>
</ports>
<volumes>
<bind>
<volume>/var/run/docker.sock:/var/run/docker.sock</volume>
</bind>
</volumes>
<autoRemove>true</autoRemove>
</run>
</image>
The challange is that there must be feed with a hartbeat towards Ryuk container at least 1 per 10s through a TCP socket containing deathnote, eg. like this: printf "label=killme" | nc localhost 8080
.
This is easy to achieve with below maven exec plugin and a simple setup-ryuk.sh
script calling the command mentioned above. (NOTE: this provides only single deathnote with no consecutive heartbeats so Ryuk dies by itself in next 10s - at that time it will reap all items for which it received a note).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>stop-postgres</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<target>
<exec executable="bash">
<arg value="setup-ryuk.sh"/>
<arg value="${ryuk.port}"/>
</exec>
</target>
</configuration>
</plugin>
To make this platform independent (as the above is valid for Linux/Mac) and to keep Ryuk alive for longer time the best way seems to be to come up with own maven plugin sending messages to TCP socket.