How to automatically feed in Docker environment NET program to generate Dump

Posted by Pantho on Mon, 31 Jan 2022 15:26:44 +0100

preface

Previously, the "first-line code farmer" wrote an article on how to dump automatically under windows. It happened that he had a demand for dump under the docker environment. Therefore, this article is based on the article of the boss.

tool

dotnet-dump (https://docs.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump)
ProcDump for linux (https://github.com/Sysinternals/ProcDump-for-Linux)

dotnet-dump

Dotnet dump is officially launched by Microsoft NET global tools, installation and use are very simple.
Installation:

dotnet tool install --global dotnet-dump

use:

dotnet-dump collect --process-id 1902 # pid

But it can't dump automatically (or I don't know), so this article mainly discusses the following tool

ProcDump for linux

This tool is a community linux porting version of ProcDump, but the main developers are also Microsoft employees.
It can automatically dump according to CPU occupation, memory occupation, number of threads, etc., so that we can deal with the scene where the dump file needs to be analyzed when the program is abnormal.

install
Add the following command to Dockerfile:
Note that we should add it to the runtime image, and it is better to build a basic image instead of installing it every time.

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0

# Install required dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        wget \
        gdb \
        lldb

# Install procdump
RUN wget https://packages.microsoft.com/repos/microsoft-debian-buster-prod/pool/main/p/procdump/procdump_1.1.1-220_amd64.deb -O procdump.deb \
    && dpkg -i procdump.deb \
    && rm procdump.deb

This article is based on the aspnet:5.0 image, i.e. debian 10. If it is based on other images, you can find the corresponding package in the following directory
https://packages.microsoft.com/repos/
Also refer to the installation instructions provided by the author

function
Because the docker container can not easily execute multiple processes at the same time at startup, we need an sh file to execute dotnet and procdump at the same time at startup.
Because I personally don't like to rely on other files besides Dockerfile, I directly created the sh file in Dockerfile

RUN echo "#!/bin/bash \n\
procdump -M 200 -w dotnet & \n\
dotnet \$1 \n\
" > ./start.sh
RUN chmod +x ./start.sh
ENTRYPOINT ["./start.sh", "<YourApp>.dll"]

If necessary, you can also create a start SH, the content is

#!/bin/bash
procdump -M 200 -w dotnet &
dotnet $1

Dockerfile changed to

COPY start.sh ./start.sh
RUN chmod +x ./start.sh
ENTRYPOINT ["./start.sh", "<YourApp>.dll"]

In this way, when docker run s, dotnet and procdump will be started at the same time, and when the memory is greater than 200M, it will dump automatically.
It should also be noted that -- privileged needs to be added when docker runs to improve permissions. For example, docker run --privileged -it xx

The parameters of procdump are

Usage: procdump [OPTIONS...] TARGET
   OPTIONS
      -h          Prints this help screen
      -C          When CPU Exceeds or equals the specified value (0 to 100 * nCPU)Core dump generation is triggered when.
      -c          When CPU Less than the specified value (0 to 100) * nCPU)Core dump generation is triggered when.
      -M          When the memory commit exceeds or equals the specified value(MB)Trigger core dump generation when
      -m          When the memory commit is less than the specified value( MB)Core dump generation is triggered when.
      -T          Triggered when the number of threads exceeds or equals the specified value.
      -F          Triggered when the file descriptor count exceeds or equals the specified value.
      -I          Polling frequency in milliseconds (default is 1000)
      -n          Number of core dumps to write before exiting (default is 1)
      -s          Consecutive seconds before dump is written (default is 10)
      -d          Writes diagnostic logs to syslog
    TARGET Choose one of the following:
      -p          Process pid
      -w          Name of the process

For example, the following command means to create a dump file when the CPU utilization is > = 65% or the memory is > = 100 MB

procdump -C 65 -M 100 -p 1234

other

Persistence of dump file
As we all know, if the docker container disappears, the dump file in it will also disappear.
Therefore, you need to output the dump file to a specified volume that has been persistently mounted. Unfortunately, at present, procdump for linux does not have an output parameter to control the output directory, which will only be generated in the same level directory of the application, so you need to move it manually.
I think PR has been mentioned. In the future, a -o parameter will be added to control the output.

reference resources

https://www.cnblogs.com/huangxincheng/p/14661031.html
https://docs.docker.com/config/containers/multi-service_container/
https://github.com/msbrz/procdump-docker

Topics: ASP.NET Docker .NET dump