0

This is no longer an issue

The problem was most likely caused by some caching problem in Visual Studio. It was fixed by recreating the solution and re-adding Docker support.

=========================================================================

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?

UPDATE Apparently it is currently (version 3.1 preview) not possible to set environment variables that outlive the application on mac and linux: https://stackoverflow.com/a/44248788/2137237

Markus Deibel
  • 1,261
  • 20
  • 26
  • It's impossible for *any* application to set an environment variable that outlives its own process. The "environment" part of the "environment variable" refers to the environment of the process and its children. – omajid Nov 21 '19 at 17:29
  • 1
    Can you try this with a hello-world style application? Only do an `Environment.Exit(1)` in `Main`. Please include the entire project file, source code dockerfile and how you are building and running the dockerfile. – omajid Nov 21 '19 at 23:49
  • I did and the disturbing truth is, that the MCVE does what I want. The docker container exits with the hardcoded value in `Environment.Exit()`. I guess I'll have to rebuild the whole solution. Adding my files step by step and then redo the Docker and Compose support :( – Markus Deibel Nov 22 '19 at 06:44

2 Answers2

2

It should be the exit status of the process, as there is no code like 0x0 from docker exit code, so it is the dotnet that return 0x0. You can look into these link.

The program '[3984] dotnet.exe' has exited with code -2147450751 (0x80008081)

The thread has exited with code 0 (0x0) with no unhandled exception

Here is the list of docker exit code.

125: docker run itself fails
126: contained command cannot be invoked
127: if contained command cannot be found
128 + n Fatal error signal n:
130 = (128+2) Container terminated by Control-C
137 = (128+9) Container received a SIGKILL
143 = (128+15) Container received a SIGTERM

What is the authoritative list of Docker Run exit codes?

Here is the simplest way to reproduce using nodejs

FROM node:alpine
COPY . /app
WORKDIR /app
RUN chmod +x /app/start.sh
entrypoint /app/start.sh

start.sh

#!/bin/sh
node start.js

js script

let exitCode = 45;
console.log("server is going to shutdown");
process.exit(exitCode)

So I can see the correct code.

enter image description here

So the issue is not related to docker, but the dotnet process it self.

Adiii
  • 54,482
  • 7
  • 145
  • 148
0

Docker always return the exit code of the process that was executed, unless some other errors were detected related to the daemon itself. This means that dotnet DataComparerCli.dll should be returning the correct exit code.

The problem is probably the way you are running the container. How are you doing so? If you use detached mode -d, the exit code will always be 0 for the process.

For example:

$ docker run busybox /bin/sh -c 'exit 3' 
$ echo $?
# 3
$ docker run -d busybox /bin/sh -c 'exit 3'
$ echo $?
# 0