2

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.

Kirill
  • 6,762
  • 4
  • 51
  • 81

2 Answers2

6

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

bsideup
  • 2,845
  • 17
  • 21
  • Here's a [gist implementing this solution](https://gist.github.com/dnault/01f9e4459a558d7c0928da33b6652a9d). Would it make sense to include something like this in Testcontainers? – dnault Sep 06 '18 at 19:00
1

You can use inspectExecCmd(execId) to get information about the executed command, and also you can get exit code from the response of inspectExecCmd