Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output from .NET Core console app by docker logs

Should I see console output (Console.WriteLine) from my .NET Core console app dockerized (linux image) by using docker logs command?

like image 876
dawid Avatar asked Dec 29 '17 22:12

dawid


2 Answers

Found out that in debug mode the app only writes to the debug output console. So if you run docker logs on a local debugging container there will be nothing. If you run the container in release mode then the logs will be present when running a docker logs

like image 69
Cale Avatar answered Sep 18 '22 19:09

Cale


Want to extend @Cale's Answer.

Like he said:

  • with VS-Debugging: Docker Logs do NOT work
  • Building and Starting the image without VS: Docker Logs work

Let's look into the containers, to understand whats happening.

Basic knowledge: docker logs will be fed from the stdout and stderr of the process started by the defined entry-point. Defined via Dockerfile and/or docker-compose.yml.

"regular docker usecase" after docker build, no Debugging

After building the image (without VS) an starting it via docker run, it looks like this.

$ docker build -t my-project:dev Dockerfile .

$ docker inspect -f '{{json .Config.Entrypoint}}' my-project:dev
["dotnet","MyProject.dll"] 

The Entrypoint is our application, so docker will read the stdout and stderr of our application and create logs.

Debug

Short: docker-image entry-point is tail -f /dev/null -> no docker logs, never ever. After the container is running w/o your app running, VS-Debug starts the application via docker exec MyAppContainer 'sh -c vsdbg ...' -> all output (stdout, stderr, debug) goes to VS

tldr;

Here it get's interesting ;)

VS Debug will create a container, which looks like this:

docker inspect -f '{{json .Config.Entrypoint}}' MyProjectName
["tail","-f","/dev/null"]

The allready mentioned tail -f /dev/null entrypoint. Docker will read from this process > therefore nothing. Actually our app isn't started at all via regular docker mechanics!

So how is out application debug sesseion started? Let's connect to the image and take a look.

root@2e65795bcd7e:/app# pstree -p 0 -A -T -a
?,
 |-sh,1394 -c...
 |   `-vsdbg,1401 --interpreter=vscode
 |       `-dotnet,1414 --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages /app/bin/Debug/net5.0/MyApp.dll
 `-tail,1 -f /dev/null

At the bottom is our entrypoint started tail -f /dev/null And on top is a sh -c vsdbg ... dotnet ... MyApp.dll. When you stop and start debugging you can see how this process disapears and reapears.

So VS does something like this:

docker exec MyAppContainer 'sh -c vsdbg --interpreter=vscode'

which in turn will launch your app as a child

dotnet --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages /app/bin/Debug/net5.0/MyApp.dll
like image 26
juwens Avatar answered Sep 19 '22 19:09

juwens