Profile Application in Docker Container
To profile a .NET application running inside a Docker container, use the dotMemory 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 dotMemory command-line tool, for example, to the
dotMemoryclt
folder:apt-get update -y && apt-get install -y wget && \ wget -O dotMemoryclt.zip https://www.nuget.org/api/v2/package/JetBrains.dotMemory.Console.linux-x64 && \ apt-get install -y unzip && \ unzip dotMemoryclt.zip -d ./dotMemoryclt && \ chmod +x -R dotMemoryclt/*Here:
apt
is the package manager for Debian-based systems. If your OS uses other package manager, use it instead ofapt
.JetBrains.dotMemory.Console.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 instantly get a snapshot of the process with PID
1
:./dotMemoryclt/tools/dotmemory get-snapshot 1To attach to the process with PID
1
:./dotMemoryclt/tools/dotmemory attach 1To get snapshots, use stdin messages:
##dotMemory["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 workspace file to the host machine. To do this, run the following command on the host machine:
docker cp {container_ID_or_name}:/{path_to_workspace_file} {path_to_folder_on_host_machine}For example:
docker cp my-aspnet-app-container:/app/[1]-dotnet.2023-03-24T16-10-18.136.dmw "/home/username/Documents/Workspaces"