This article explains how to use Visual Studio to step through your code when it is running in a local Docker container.
Before you begin
This guide assumes you're able to build and run custom Sitecore runtime images which include your solution's build output.
You can use the custom-images solution from the Docker Examples repository if you don't have your own. Just follow the preparation steps or run the included
init.ps1 script to perform these preparation steps automatically.
To debug apps in a local Docker container, you will need the following:
- Visual Studio 2019 version 16.5.0 or later.
Ensure debug symbols available
Though not unique to containers, you'll need to ensure that debug symbols have been deployed to the container.
This may already be done if you're building your solution image with "Debug" build configuration. For example, the custom-images example in the Docker Examples repository uses a
BUILD_CONFIGURATION environment variable which sets this to "Debug" by default.
You could also perform a "Debug" file deployment from Visual Studio into the running container.
Attach the Visual Studio debugger
In Visual Studio, you have 2 options for attaching to a process in a container.
Option 1 - Use the Containers window
If the Containers window is not showing, open it from the top View menu. Select Other Windows, then Containers.
In the list of running containers, right-click on the one you'd like to debug, and click Attach to Process.
The Attach to Process dialog appears and shows the available processes that are running in the container.
Select the process (e.g. w3wp.exe) as usual (check Show processes from all users if not visible), and click Attach.
For more information about the Containers window, see the Microsoft documentation.
Option 2 - Use the Debug menu
A few more steps than the Containers window, but you can also use the Debug menu.
- From the top Debug menu, select Attach to Process... to open the Attach to Process dialog
- For Connection type, select Docker (Windows Container)
- For Connection target, click the Find... button. Running containers will appear in the list. Select the container you’d like to debug and click OK.
- For Attach to, ensure Managed Code (v4.6, v4.5, v4.0) is selected (this should be the default).
- Select the process (e.g. w3wp.exe) as usual (check Show processes from all users if not visible), and click Attach