How can I pass the exit code of a dotnet core application out of a docker container

I have a small dotnet core application that is supposed to help with end to end tests.

To be able to integrate it with our CI pipeline I put it into a docker container
and up to this point things are working out OK.

Now the problem is that docker container does not exit with the same code as the application does.

I assume this is because the application is encapsulated by the dotnet runtime, so even if the application exits with a non-zero code the actual entry point (dotnet) as defined in the Dockerfile

ENTRYPOINT ["dotnet", "DataComparerCli.dll"]

exits with 0x0 and this is what is used as the exit code of the container.

I tried to work around this by having the application store its exit code in an environment variable and creating a shell script as entry point which then calls dotnet in turn and uses the application’s exit code as its own but this currently fails.

#!/bin/bash

dotnet /app/path/to/dll/DataComparerCli.dll
exit $DataComparerExitCode

It feels like I’m using a (broken) crutch and that this is the reason why I can’t get this to work.

Is this the a good approach and I’m just having issues with the bash script or are there better ways to get this to work?

Source: StackOverflow