Blog Post

Microsoft Developer Community Blog
3 MIN READ

Debugging Docker Containers (attaching)

chilberto's avatar
chilberto
Iron Contributor
Feb 13, 2019

First published on MSDN on Jan 30, 2019

Authored by Jeffrey Chilberto

Visual Studio has excellent built in support for working with Docker containers, and most of the examples on the web are written with those tools installed. But what about the situation where the container is already running? This post will provide an example of debugging a .Net Framework application running a windows container as well as provide references to posts about remote debugging for different platforms including the .Net Framework, Python, and .Net Core applications.

 

General


In all three examples, the container needs to be prepared to allow for Visual Studio to access a running process on the container. Most devs are familiar with debugging solutions at design time using both the auto-attach (F5) and attaching the debugger to a running process on their local machine. This approach can be used to attach to a remote computer and even to Azure Web Apps and Azure Functions .

Attaching to a container is similar to those examples where the ability to remote debug needs to be enabled on the container to allow Visual Studio the ability to connect.

 

.Net Framework


The first step to enabling the debug of a .Net Framework app is to install and run the remote debugger. The following is an example of enabling this on a dockerfile that uses Microsoft's Asp.Net image:

 

FROM microsoft/aspnet:10.0.14393.2665

EXPOSE 4020 4021
# download the remote debugger
RUN Invoke-WebRequest -OutFile c:\rtools_setup_x64.exe -Uri http://download.microsoft.com/download/1/2/2/1225c23d-3599-48c9-a314-f7d631f43241/rtools_setup_x64.exe;
# install the remote debugging
RUN & 'c:\rtools_setup_x64.exe' /install /quiet

# rest of the docker file

After the image has been built, the next step is to make sure the ports for remote debugging (-p 4020:4020 -p 4021:4021) are also mapped when running the container. For example, the following starts the container with the name "mysite" and exposes ports 4020 and 4021 and port 80 as port 8078.

docker run -d -p 4020:4020 -p 4021:4021 -p 8078:80 --name mysite core

The final step is to start the remote debugger on the container as shown below:

docker exec -it mysite "C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger\x64\msvsmon.exe" /nostatus /silent /noauth /anyuser /nosecuritywarn

Within Visual Studio, select the Attach to Process action in the Debug window:



In the Transport, Remote is used and the Find button is used to establish the remote connection:



The screenshot below shows the detected containers:



If there is more than one container detected, you can determine the specific IP address of the container by using the docker inspect command:

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" mysite

Once the container has been selected, then the running process can be selected. For example, if debugging a IIS web application:



Hope this helps! Please post any helpful comments for others and any other links you might know of!

References

.Net Framework
Another example of remote debugging .Net Framework applications can be found in post by Richard Banks: How to Remote Debug a .NET App in a Windows Docker Container .
.Net Core
Offroad Debugging of .NET Core on Linux OSX from Visual Studio

Debug .NetCore Containers in Docker and Kubernetes
Python
Python debug configurations in Visual Studio Code

Updated Mar 26, 2020
Version 3.0

3 Comments

  • jspielvogel's avatar
    jspielvogel
    Copper Contributor

    This does not work.  rtools_setup_x64.exe /install /quite doesn't even create a folder under program files. After running it a couple times, it did create the visual studio 14 folder and subfolders, but there is no "msvsmon.exe" file under the x64 folder or anywhere else. There is a 1033 folder, and 3 files: "VsGraphicsCaptureEngine.dll", "VsGraphicsHelper.dll", and "VsGraphicsRemoteEngine.exe".

  • SlateyDev's avatar
    SlateyDev
    Copper Contributor
    Invoke-WebRequest uses powershell. You need set the shell to powershell in order to run it or run powershell to run the command.
     
    Set the shell with the following at the top (under your FROM line)
    SHELL ["powershell"]
  • Geedsen's avatar
    Geedsen
    Copper Contributor
    Any idea why this does not work for me, it does not recognize Invoke-Webrequest Step 4/20 : EXPOSE 443 ---> Using cache ---> 13d750f12806 Step 5/20 : EXPOSE 4020 4021 ---> Using cache ---> c802ee81fa63 Step 6/20 : RUN Invoke-WebRequest -OutFile c:\rtools_setup_x64.exe -Uri http://download.microsoft.com/download/1/2/2/1225c23d-3599-48c9-a314-f7d631f43241/rtools_setup_x64.e...; ---> Running in 1283009f28c9 'Invoke-WebRequest' is not recognized as an internal or external command, operable program or batch file.