Should I see console output (Console.WriteLine) from .net core console app dockerized (linux image) by using docker logs command?
Want to extend @Cale's Answer.
Like he said:
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
.
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.
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
Yes. You can see logs written to stdout and stderr if you use the default logging driver.
By default, docker logs shows the command’s STDOUT and STDERR.
View logs for a container or service
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