Search code examples
docker-javatestcontainers

How can I Get command exit code executed with Testcontainers?


Using GenericContainer#execInContainer I can only get stdout or stderr.

Is there any way to get exit code of executed command?

I can't rely on the presence of text in stderr. The application I execute prints some info to stderr but exits with code 0.


Solution

  • execInContainer is just a shortcut to execCreateCmd/execStartCmd from docker-java. Unfortunately, their API doesn't provide a way to get the exit code.

    But you can make use of built-in shell functionality and just return the code as part of stdout/stderr:

    $ sh -c 'false; echo "ExitCode=$?"'
    ExitCode=1
    

    where false is your command