22 Feb 2021 ~ 2 min read

Debugging Docker containers running .NET

Debugging Docker containers running .NET

Ever had a memory leak and wanted to diagnose it? Luckily there are some free tools from Microsoft that you can install and use!

This is a two part series:

Modify your Dockfile to use .NET SDK

For example here we're changing from aspnet 3.1 to sdk 3.1. This allows us to install the debugging tools later on

-FROM mcr.microsoft.com/dotnet/aspnet:3.1.10-alpine3.12 AS base
+FROM mcr.microsoft.com/dotnet/sdk:3.1.404-alpine3.12 AS base

Launch a terminal for a running container

There are a few ways to do this for example using Visual Studio or jumping into a CLI environment on the Docker container.

While in Visual Studio and debugging, navigate to the 'Containers' window and select 'Open Terminal Window'

Install the tools

Now, having access to the container using CLI, install tools such as dotnet-counters, donet-dump ot dotnet-trace. See Debugging a memory leak for more information.

dotnet tool install --global dotnet-trace
dotnet tool install --global dotnet-counters
dotnet tool install --global dotnet-dump
export PATH="$PATH:/root/.dotnet/tools"


These example are basically taken from Debugging a memory leak. Each command will generate a file you can open in Visual Studio once generated. To generate, Ctrl+C on the process to end it.


dotnet-trace collect --process-id $(pidof dotnet) --providers Microsoft-DotNETCore-SampleProfiler

Observe Memory

This will monitor memory usage in real time but not generate a file.

dotnet-counters monitor --process-id $(pidof dotnet) --refresh-interval 1

Memory dump collection

This will generate file to analyze memory usage with.

dotnet-dump collect --process-id $(pidof dotnet)

To analyze the memory dump file you can refer again to Debugging a memory leak.

Headshot of Jason Watson

Hi, I'm Jason. I'm a software engineer and architect. You can follow me on Twitter, see some of my work on GitHub, or read more about me on my website.