Using <wait><exit>0</exit></wait> fails with error: Container stopped with exit code null unexpectedly
Description
I want to run a container in some phase of the Maven build, to do something and then exit - before the next Maven task runs.
I have a Dockerfile and verified that if I build an image with it and run my process, it terminates with a 0 exit code (assuming there are no problems in the container process itself).
Using this configuration in the <run> section:
<run>
<autoRemove>true</autoRemove>
...
<wait>
<time>20000</time>
<exit>0</exit>
</wait>
...
</run>
When the container exits after a successful run, in under 20 seconds, Maven fails the build with these messages:
[ERROR] DOCKER> [name:tag]: Container stopped with exit code null unexpectedly after 8804 ms while waiting on exit code 0
[ERROR] DOCKER> Error occurred during container startup, shutting down...
[ERROR] DOCKER> (Unable to stop container id [a3b8e55b45ba] : {"cause":"no such container","message":"no container with name or ID \"a3b8e55b45ba\" found: no such container","response":404} (Not Found: 404))
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.165 s
[INFO] Finished at: 2022-12-01T16:25:07+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.40.2:start (build-angular-docker) on project rtp-control: (Unable to stop container id [a3b8e55b45ba] : {"cause":"no such container","message":"no container with name or ID \"a3b8e55b45ba\" found: no such container","response":404} (Not Found: 404)) -> [Help 1]
I tried to use <exit></exit> under the assumption that this would mean "wait until the container exits with any code" but this doesn't wait - it just continues without waiting for the container to stop (and then some of my other tasks fail because the container didn't generate the required artifacts).
Info
- docker-maven-plugin version : 0.40.2
- Maven version (
mvn -v) :
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.5, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_IL, platform encoding: UTF-8
OS name: "linux", version: "5.19.0-23-generic", arch: "amd64", family: "unix"
- Docker version : 20.10.21
After more checking, this failure only happens if I use <autoRemove>true</autoRemove>. The failure happens (apparently) when the task tries to remove the container after it was already auto-removed.
If I set <autoRemove>false</autoRemove>, then it leaves the container in the system - and I will get a failure when I run the build again because it won't be able to create a container with the same name.
The workaround I have is to use set <run> with both <autoRemove>false</autoRemove> and <wait><exit>0</exit></wait>, and make sure to set both <goal>start</goal> and <goal>stop</goal>.
I'm still very confused about the error message "Container stopped with exit code null unexpectedly"
Thanks for your investigation!
I'm still very confused about the error message "Container stopped with exit code null unexpectedly"
Indeed, for me it smells like that there is confusion between null and 0. Unfortunately I don't have time to investigate this further (but maybe @rohanKanojia ?). Happy to take an PRs, too!
Also not sure why it tries to remove the container twice if you don't use docker:stop but only autoRemove==true ?
Another workaround btw could be to keep autoRemove on but use a containerNamePattern in the build config, that leverages the %i index as described in https://dmp.fabric8.io/#container-name, too avoid naming collisions.
@guss77 : Do you have any reproducer project in which I can reproduce this issue?