Profile Application in Docker Container
To profile a .NET application running inside a Docker container, use the dotTrace command-line profiler. To download the profiler and start a profiling session, you should have access to the container shell.
Log in to the host machine running Docker.
To be able to communicate with the application container, we must know either its ID or name. To get them, run:
docker psSample output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e58110265dd6 my-aspnet-app "dotnet MyAspNetApp.…" 22 hours ago Up 22 hours 0.0.0.0:5142->5142/tcp my-aspnet-app-containerHere
e58110265dd6
is the container ID andmy-aspnet-app-container
is the name.Start a shell session inside the Docker container. You can use different command-line interpreters depending on the image OS, for example,
sh
or/bin/bash
.docker exec -it {container_ID_or_name} shFor example:
docker exec -it my-aspnet-app-container shIn the container, download and unzip the dotTrace command-line tool, for example, to the
dotTrace
folder:apt-get update -y && apt-get install -y wget && \ wget -O dotTraceclt.zip https://www.nuget.org/api/v2/package/JetBrains.dotTrace.CommandLineTools.linux-x64 && \ apt-get install -y unzip && \ unzip dotTraceclt.zip -d ./dotTrace && \ chmod +x -R dotTrace/*Here:
apt
is the package manager for Debian-based systems. If your OS uses other package manager, use it instead ofapt
.JetBrains.dotTrace.CommandLineTools.linux-x64
is the package name for the Linux x64 systems. This name will vary depending on a target platform. Here you can find the full list of packages for various platforms.
Before you can attach to the process you want to profile, you must get the process ID:
ps auxIf the
ps
command is not available in your Linux distribution, install it with:apt-get update && apt-get install procpsSample output:
# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 1.2 276055932 200960 ? Ssl Mar23 0:21 dotnet MyAspNetApp.dll --urls http://*:5142 root 914 0.0 0.0 2480 580 pts/0 Ss 05:33 0:00 sh root 2171 0.0 0.0 6756 2940 pts/0 R+ 12:55 0:00 ps auxProfile the process. For example, to attach to the process with PID
1
and use stdin message to get snapshots:./dotTrace/tools/dottrace attach 1 --service-input=stdinTo get a snapshot, send:
##dotTrace["get-snapshot"]For more information about working with the command-line tool, refer to the corresponding topic.
To stop profiling and save collected data, press Ctrl+C.
To end the shell session in the container, run
exit
.Copy the snapshot files to the host machine. Note that dotTrace saves snapshot as multiple files, e.g.,
somename.dtp
,somename.dtp.0000
,somename.dtp.0001
, and so on. Therefore, you should first put them to an archive in the container. On the host machine:Create an archive:
docker exec my-aspnet-app-container sh -c "tar -czf /tmp/snapshot.tar.gz /app/somename.dtp*"Copy the archive to the host:
docker cp my-aspnet-app-container:/tmp/snapshot.tar.gz /home/username/Snapshots/snapshot.tar.gzUnpack the archive:
tar -xzf /home/username/Snapshots/snapshot.tar.gz -C /home/username/Snapshots